简体   繁体   中英

How to find standard deviation of a variable in dataframe with 5 lists in R

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

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 acheive 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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM