-4

我有一个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 

我想为每个名字执行一些基本统计数据(平均值、标准差等),即:约翰身高、体重等的平均值;苏珊、身高、体重等的平均值。

为此,我首先编写了一个函数:

 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)但没有奏效。

4

1 回答 1

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
于 2013-09-19T17:20:18.057 回答