2

I am an R newbie, and I would like to divide every value in the 168 columns of my dataset by the SD of the respective column.

I tried:

new<- for (i in 1:168 (df))

function(x){
  x/sd(x, na.rm=TRUE)
}
}

But it didn't work. It is the first time I am working with loops or functions. Therefore, I would be happy for your help. This is an example from my dataset (3columns). There are 168 columns, and the 169th column has the IDs.

structure(list(var1 = c(1, 2, 35, 5, 22, 26.5, 31, 35.5, 40, 
44.5, 49, 53.5, 58, 62.5, 67, 71.5, 76, 80.5, 85), var2 = c(1, 
2, 4, 5.33333333333333, 6.83333333333333, 8.33333333333333, 9.83333333333333, 
11.3333333333333, 12.8333333333333, 14.3333333333333, 15.8333333333333, 
17.3333333333333, 18.8333333333333, 20.3333333333333, 21.8333333333333, 
23.3333333333333, 24.8333333333333, 26.3333333333333, 27.8333333333333
), var3 = c(2, 1, 3, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 
8, 8.5, 9, 9.5, 10, 10.5)), row.names = c(NA, -19L), class = c("tbl_df", 
"tbl", "data.frame"))

Thanks, Caro

Carolin V
  • 51
  • 10
  • 2
    ``lapply(df, function(x) x/sd(x, na.rm=TRUE)`` – user438383 Aug 25 '21 at 14:02
  • 2
    Does this answer your question? [Apply function to each column in a data frame observing each columns existing data type](https://stackoverflow.com/questions/7303322/apply-function-to-each-column-in-a-data-frame-observing-each-columns-existing-da) – user438383 Aug 25 '21 at 14:03
  • Possible duplicate of https://stackoverflow.com/questions/7303322/apply-function-to-each-column-in-a-data-frame-observing-each-columns-existing-da – akrun Aug 26 '21 at 15:58

2 Answers2

5

In dplyr, you can use across to apply a function to multiple columns.

library(dplyr)
df <- df %>% mutate(across(starts_with('var'), ~./sd(.)))
df

#    var1  var2  var3
#    <dbl> <dbl> <dbl>
# 1 0.0384 0.118 0.707
# 2 0.0767 0.237 0.354
# 3 1.34   0.474 1.06 
# 4 0.192  0.632 1.06 
# 5 0.844  0.809 1.24 
# 6 1.02   0.987 1.41 

In base R, we can use lapply -

df[] <- lapply(df, function(x) x/sd(x))

To apply this to selected columns (1:168) you can do

df[1:168] <- lapply(df[1:168], function(x) x/sd(x))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Adding to this, you can use `across(everything())` if you really want to apply the function to every column – Matt Kaye Aug 25 '21 at 14:15
2

We could use map_dfr from purrr package:

libraray(purrr)
map_dfr(df, ~./sd(.))

Output:

     var1  var2  var3
    <dbl> <dbl> <dbl>
 1 0.0384 0.118 0.707
 2 0.0767 0.237 0.354
 3 1.34   0.474 1.06 
 4 0.192  0.632 1.06 
 5 0.844  0.809 1.24 
 6 1.02   0.987 1.41 
 7 1.19   1.16  1.59 
 8 1.36   1.34  1.77 
 9 1.53   1.52  1.94 
10 1.71   1.70  2.12 
11 1.88   1.88  2.30 
12 2.05   2.05  2.47 
13 2.22   2.23  2.65 
14 2.40   2.41  2.83 
15 2.57   2.59  3.01 
16 2.74   2.76  3.18 
17 2.92   2.94  3.36 
18 3.09   3.12  3.54 
19 3.26   3.30  3.71 
TarJae
  • 72,363
  • 6
  • 19
  • 66