0

I wanted to calculate the standard deviation for each column in my data set below:

    a b c
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

I tried creating a loop like this:

for (x in 1:3){
  sdcol=sd(data[,x])
}

But I get the following error:

Error in data[, x] : object of type 'closure' is not subsettable

Can you help me with such a loop?

Thanks

Ric S
  • 9,073
  • 3
  • 25
  • 51
Debutant
  • 355
  • 5
  • 17

2 Answers2

0

EDIT

I suggest you try this command, which is faster than a for cycle. Be sure that your variables are all numeric. The na.rm = TRUE argument is useful if your columns contain missing values

sapply(data, sd, na.rm = TRUE)

Example

sapply(iris[,1:4], sd, na.rm = TRUE)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
   0.8280661    0.4358663    1.7652982    0.7622377 
Ric S
  • 9,073
  • 3
  • 25
  • 51
  • hi thank you for the answer, I apply it and get this error: > sapply(x[,1:3], sd) Error: C stack usage 15923808 is too close to the limit – Debutant May 26 '20 at 09:26
  • How big is your dataset? Is your workspace full of objects, or your RAM full? – Ric S May 26 '20 at 09:43
  • I run the command (> sapply(data[,1:3], sd)) But I get NA values: [1] NA NA NA NA NA NA NA NA NA – Debutant May 26 '20 at 09:54
  • It's because you have missing values in your columns. You need to add `na.rm = TRUE` that will be passed to function `sd`. I update my answer above. – Ric S May 26 '20 at 10:02
0

We can also use

library(dplyr)
iris %>%
     summarise_at(1:4, sd, na.rm = TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662