I am experimenting with writing functions and I was wondering how you apply a function to columns individually. For example, with the mtcars dataset, I would like to compute the z value for each column ( (x - mean(column of x ))/sd(column of x). How do I do that part 'column of x', because I would like to do it for each column individually instead of writing out mtcars$mpg each time for example.
CodePudding user response:
Another option is using the scale
function which returns the z score of each column. Here an example using the mtcars
dataset:
scale(mtcars)
Output:
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 0.15088482 -0.1049878 -0.57061982 -0.53509284 0.56751369 -0.610399567 -0.77716515 -0.8680278 1.1899014 0.4235542 0.7352031
Mazda RX4 Wag 0.15088482 -0.1049878 -0.57061982 -0.53509284 0.56751369 -0.349785269 -0.46378082 -0.8680278 1.1899014 0.4235542 0.7352031
Datsun 710 0.44954345 -1.2248578 -0.99018209 -0.78304046 0.47399959 -0.917004624 0.42600682 1.1160357 1.1899014 0.4235542 -1.1221521
Hornet 4 Drive 0.21725341 -0.1049878 0.22009369 -0.53509284 -0.96611753 -0.002299538 0.89048716 1.1160357 -0.8141431 -0.9318192 -1.1221521
Hornet Sportabout -0.23073453 1.0148821 1.04308123 0.41294217 -0.83519779 0.227654255 -0.46378082 -0.8680278 -0.8141431 -0.9318192 -0.5030337
Valiant -0.33028740 -0.1049878 -0.04616698 -0.60801861 -1.56460776 0.248094592 1.32698675 1.1160357 -0.8141431 -0.9318192 -1.1221521
Duster 360 -0.96078893 1.0148821 1.04308123 1.43390296 -0.72298087 0.360516446 -1.12412636 -0.8680278 -0.8141431 -0.9318192 0.7352031
Merc 240D 0.71501778 -1.2248578 -0.67793094 -1.23518023 0.17475447 -0.027849959 1.20387148 1.1160357 -0.8141431 0.4235542 -0.5030337
Merc 230 0.44954345 -1.2248578 -0.72553512 -0.75387015 0.60491932 -0.068730634 2.82675459 1.1160357 -0.8141431 0.4235542 -0.5030337
Merc 280 -0.14777380 -0.1049878 -0.50929918 -0.34548584 0.60491932 0.227654255 0.25252621 1.1160357 -0.8141431 0.4235542 0.7352031
Merc 280C -0.38006384 -0.1049878 -0.50929918 -0.34548584 0.60491932 0.227654255 0.58829513 1.1160357 -0.8141431 0.4235542 0.7352031
Merc 450SE -0.61235388 1.0148821 0.36371309 0.48586794 -0.98482035 0.871524874 -0.25112717 -0.8680278 -0.8141431 -0.9318192 0.1160847
Merc 450SL -0.46302456 1.0148821 0.36371309 0.48586794 -0.98482035 0.524039143 -0.13920420 -0.8680278 -0.8141431 -0.9318192 0.1160847
Merc 450SLC -0.81145962 1.0148821 0.36371309 0.48586794 -0.98482035 0.575139986 0.08464175 -0.8680278 -0.8141431 -0.9318192 0.1160847
Cadillac Fleetwood -1.60788262 1.0148821 1.94675381 0.85049680 -1.24665983 2.077504765 0.07344945 -0.8680278 -0.8141431 -0.9318192 0.7352031
Lincoln Continental -1.60788262 1.0148821 1.84993175 0.99634834 -1.11574009 2.255335698 -0.01608893 -0.8680278 -0.8141431 -0.9318192 0.7352031
Chrysler Imperial -0.89442035 1.0148821 1.68856165 1.21512565 -0.68557523 2.174596366 -0.23993487 -0.8680278 -0.8141431 -0.9318192 0.7352031
Fiat 128 2.04238943 -1.2248578 -1.22658929 -1.17683962 0.90416444 -1.039646647 0.90727560 1.1160357 1.1899014 0.4235542 -1.1221521
Honda Civic 1.71054652 -1.2248578 -1.25079481 -1.38103178 2.49390411 -1.637526508 0.37564148 1.1160357 1.1899014 0.4235542 -0.5030337
Toyota Corolla 2.29127162 -1.2248578 -1.28790993 -1.19142477 1.16600392 -1.412682800 1.14790999 1.1160357 1.1899014 0.4235542 -1.1221521
Toyota Corona 0.23384555 -1.2248578 -0.89255318 -0.72469984 0.19345729 -0.768812180 1.20946763 1.1160357 -0.8141431 -0.9318192 -1.1221521
Dodge Challenger -0.76168319 1.0148821 0.70420401 0.04831332 -1.56460776 0.309415603 -0.54772305 -0.8680278 -0.8141431 -0.9318192 -0.5030337
AMC Javelin -0.81145962 1.0148821 0.59124494 0.04831332 -0.83519779 0.222544170 -0.30708866 -0.8680278 -0.8141431 -0.9318192 -0.5030337
Camaro Z28 -1.12671039 1.0148821 0.96239618 1.43390296 0.24956575 0.636460997 -1.36476075 -0.8680278 -0.8141431 -0.9318192 0.7352031
Pontiac Firebird -0.14777380 1.0148821 1.36582144 0.41294217 -0.96611753 0.641571082 -0.44699237 -0.8680278 -0.8141431 -0.9318192 -0.5030337
Fiat X1-9 1.19619000 -1.2248578 -1.22416874 -1.17683962 0.90416444 -1.310481114 0.58829513 1.1160357 1.1899014 0.4235542 -1.1221521
Porsche 914-2 0.98049211 -1.2248578 -0.89093948 -0.81221077 1.55876313 -1.100967659 -0.64285758 -0.8680278 1.1899014 1.7789276 -0.5030337
Lotus Europa 1.71054652 -1.2248578 -1.09426581 -0.49133738 0.32437703 -1.741772228 -0.53093460 1.1160357 1.1899014 1.7789276 -0.5030337
Ford Pantera L -0.71190675 1.0148821 0.97046468 1.71102089 1.16600392 -0.048290296 -1.87401028 -0.8680278 1.1899014 1.7789276 0.7352031
Ferrari Dino -0.06481307 -0.1049878 -0.69164740 0.41294217 0.04383473 -0.457097039 -1.31439542 -0.8680278 1.1899014 1.7789276 1.9734398
Maserati Bora -0.84464392 1.0148821 0.56703942 2.74656682 -0.10578782 0.360516446 -1.81804880 -0.8680278 1.1899014 1.7789276 3.2116766
Volvo 142E 0.21725341 -1.2248578 -0.88529152 -0.54967799 0.96027290 -0.446876870 0.42041067 1.1160357 1.1899014 0.4235542 -0.5030337
attr(,"scaled:center")
mpg cyl disp hp drat wt qsec vs am gear carb
20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750 0.437500 0.406250 3.687500 2.812500
attr(,"scaled:scale")
mpg cyl disp hp drat wt qsec vs am gear carb
6.0269481 1.7859216 123.9386938 68.5628685 0.5346787 0.9784574 1.7869432 0.5040161 0.4989909 0.7378041 1.6152000
CodePudding user response:
z_func <- function(x) {((x - mean(x, na.rm = T))/sd(x))}
library(dplyr)
iris %>%
mutate(z_sepal = z_func(Sepal.Length))
or if you want to change multiple columns
iris %>%
mutate(across(c("Sepal.Length","Petal.Width"), ~z_func(.), .names = "z_{col}"))
CodePudding user response:
Another variation:
library(tidyverse)
get_z <- function(x){
mtcars |>
mutate(z = ({{ x }} - mean({{ x}})) / sd({{ x }}))
}
get_z(mpg)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
#> Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
#> Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
#> Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
#> Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
#> Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
#> Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
#> Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
#> Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
#> Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
#> Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
#> Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
#> Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
#> Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
#> Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
#> Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
#> Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
#> Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
#> Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
#> Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
#> Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
#> AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
#> Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
#> Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
#> Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
#> Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
#> Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
#> Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
#> Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
#> Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
#> Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
#> z
#> Mazda RX4 0.15088482
#> Mazda RX4 Wag 0.15088482
#> Datsun 710 0.44954345
#> Hornet 4 Drive 0.21725341
#> Hornet Sportabout -0.23073453
#> Valiant -0.33028740
#> Duster 360 -0.96078893
#> Merc 240D 0.71501778
#> Merc 230 0.44954345
#> Merc 280 -0.14777380
#> Merc 280C -0.38006384
#> Merc 450SE -0.61235388
#> Merc 450SL -0.46302456
#> Merc 450SLC -0.81145962
#> Cadillac Fleetwood -1.60788262
#> Lincoln Continental -1.60788262
#> Chrysler Imperial -0.89442035
#> Fiat 128 2.04238943
#> Honda Civic 1.71054652
#> Toyota Corolla 2.29127162
#> Toyota Corona 0.23384555
#> Dodge Challenger -0.76168319
#> AMC Javelin -0.81145962
#> Camaro Z28 -1.12671039
#> Pontiac Firebird -0.14777380
#> Fiat X1-9 1.19619000
#> Porsche 914-2 0.98049211
#> Lotus Europa 1.71054652
#> Ford Pantera L -0.71190675
#> Ferrari Dino -0.06481307
#> Maserati Bora -0.84464392
#> Volvo 142E 0.21725341
Created on 2022-04-30 by the reprex package (v2.0.1)