具有NA值的数据框中的列的均值和SD [英] means and SD for columns in a dataframe with NA values
本文介绍了具有NA值的数据框中的列的均值和SD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用NA
值计算data.frame中几列(第一列除外)的均值和标准差.
I'm trying to calculate the mean and standard deviation of several columns (except the first column) in a data.frame with NA
values.
我已经尝试过colMeans
,sapply
等创建一个循环,遍历data.frame,然后将均值和标准差存储在单独的表中,但始终会出现"FUN"错误.任何帮助都会很棒.谢谢
I've tried colMeans
, sapply
, etc., to create a loop that runs through the data.frame and then stores means and standard deviations in a separate table but keep getting a "FUN" error. any help would be great. Thanks
a
推荐答案
sapply(df, function(cl) list(means=mean(cl,na.rm=TRUE), sds=sd(cl,na.rm=TRUE)))
col1 col2 col3 col4 col5
means 3 8 12.5 18.25 22.5
sds 1.581139 1.581139 1.290994 1.707825 1.290994
as.data.frame( t(sapply(df, function(cl) list(means=mean(cl,na.rm=TRUE),
sds=sd(cl,na.rm=TRUE))) ))
means sds
col1 3 1.581139
col2 8 1.581139
col3 12.5 1.290994
col4 18.25 1.707825
col5 22.5 1.290994
这篇关于具有NA值的数据框中的列的均值和SD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文