我有一个data.frame
,数据分为16列:首先是名称第二,日期是其他14,其中包含指标。
喜欢:
name date hight weight ....
John 1950 1.81 78
John 1948 1.60 60
Susan 1985 1.40 40 .
Susan 1995 1.45 60
我想为每个名字执行一些基本的统计数据(均值,sd等),即:John的身高,体重等的平均值;苏珊的意思,身高,体重等。
为了做到这一点,首先我写了一个函数:
mysummary <- function(x){
setNames(c(mean(x), sd(x), skewness(x), kurtosis(x)),
c("Mean", "SD", "Skewness", "Kurtosis"))
}
但是当我用命令执行它时:
summaryStatic = by(data[,c('height','weight')], list(data$name), function(x){
y <- sapply(x, FUN =mysummary(as.numeric(x)))
return(y)
})
但是我收到了以下错误:
Error in mean(x) : (list) object cannot be coerced to type 'double'
我知道这与data.frame
结构有些关联。如你所见,我试图用as.numeric(x)
解决它,但它没有用。
答案 0 :(得分:0)
我不确定,但也许这就是你想要的。如果是这样,只需添加更多摘要统计信息:
my.data <- read.table(text = '
name date height weight
John 1950 1.81 78
John 1948 1.60 60
Susan 1985 1.40 40
Susan 1995 1.45 60
', header = TRUE, stringsAsFactors = FALSE)
with(my.data, aggregate(height ~ name, FUN = function(x) c( SD = sd(x), MEAN = mean(x) ) ))
with(my.data, aggregate(weight ~ name, FUN = function(x) c( SD = sd(x), MEAN = mean(x) ) ))
with(my.data, aggregate(cbind(height, weight) ~ name, FUN = function(x) c( SD = sd(x), MEAN = mean(x) ) ))
name height.SD height.MEAN weight.SD weight.MEAN
1 John 0.14849242 1.70500000 12.72792 69.00000
2 Susan 0.03535534 1.42500000 14.14214 50.00000
with(my.data, aggregate(my.data[,3:4], by = list(name), FUN = function(x) c( SD = sd(x), MEAN = mean(x) ) ))
Group.1 height.SD height.MEAN weight.SD weight.MEAN
1 John 0.14849242 1.70500000 12.72792 69.00000
2 Susan 0.03535534 1.42500000 14.14214 50.00000