0

I have a data frame where I want to find the SD of a single variable which consists of 5 lists.

I have used the following code to calculate the mean for columns 3 and 5;

lapply(data = cf, function(x) colMeans(x[3:5]))

where cf is the dataframe with 5 lists.

is there an colMeans equivalent to calculate SD

UseR10085
  • 7,120
  • 3
  • 24
  • 54
ts22
  • 9
  • 1
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Apr 16 '21 at 07:21
  • @ts22, if our comments and answers helped you please do not forget to click the "check"/+1 button or leave a message for us to improve our answers – Paul Apr 16 '21 at 09:27

1 Answers1

0

I think you need to use apply to tell R to use sd() on the wanted columns of each list element.

Please see the example below, iris is a baseR dataset you should be able to make it run.

df <- split(iris, iris$Species) # create a toy dataset

lapply(df, function(x) colMeans(x[1:3])) # what you have done so far
# you can achieve the same thing using
lapply(df, function(x) apply(x[1:3], 2, mean))

lapply(df, function(x) apply(x[1:3], 2, sd)) # this might be what you want

The output:

> lapply(df, function(x) apply(x[1:3], 2, sd))
$setosa
Sepal.Length  Sepal.Width Petal.Length 
   0.3524897    0.3790644    0.1736640 

$versicolor
Sepal.Length  Sepal.Width Petal.Length 
   0.5161711    0.3137983    0.4699110 

$virginica
Sepal.Length  Sepal.Width Petal.Length 
   0.6358796    0.3224966    0.5518947 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Paul
  • 2,850
  • 1
  • 12
  • 37