'Referencing a column in a function in R

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.



Solution 1:[1]

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 

Solution 2:[2]

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}"))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Quinten
Solution 2 Julian