生成具有聚合函数

时间:2015-09-09 12:34:34

标签: r aggregate rename

我不明白为什么聚合函数生成的数据框的列名不能正确重命名?

我使用聚合函数按组进行一些汇总统计。在下文中,我提出了一个可重复的例子。

> data <- read.table(header=T, text='
+  subject sex condition before after change
+        1   F   placebo   10.1   6.9   -3.2
+        2   F   placebo    6.3   4.2   -2.1
+        3   M   aspirin   12.4   6.3   -6.1
+        4   F   placebo    8.1   6.1   -2.0
+        5   M   aspirin   15.2   9.9   -5.3
+        6   F   aspirin   10.9   7.0   -3.9
+        7   F   aspirin   11.6   8.5   -3.1
+        8   M   aspirin    9.5   3.0   -6.5
+        9   F   placebo   11.5   9.0   -2.5
+       10   M   placebo   11.9  11.0   -0.9
+       11   F   aspirin   11.4   8.0   -3.4
+       12   M   aspirin   10.0   4.4   -5.6
+       13   M   aspirin   12.5   5.4   -7.1
+       14   M   placebo   10.6  10.6    0.0
+       15   M   aspirin    9.1   4.3   -4.8
+       16   F   placebo   12.1  10.2   -1.9
+       17   F   placebo   11.0   8.8   -2.2
+       18   F   placebo   11.9  10.2   -1.7
+       19   M   aspirin    9.1   3.6   -5.5
+       20   M   placebo   13.5  12.4   -1.1
+       21   M   aspirin   12.0   7.5   -4.5
+       22   F   placebo    9.1   7.6   -1.5
+       23   M   placebo    9.9   8.0   -1.9
+       24   F   placebo    7.6   5.2   -2.4
+       25   F   placebo   11.8   9.7   -2.1
+       26   F   placebo   11.8  10.7   -1.1
+       27   F   aspirin   10.1   7.9   -2.2
+       28   M   aspirin   11.6   8.3   -3.3
+       29   F   aspirin   11.3   6.8   -4.5
+       30   F   placebo   10.3   8.3   -2.0
+  ')
> 
> summary.function <- function(x){c(mean(abs(x)),mean(x),min(x),max(x))}
> data.summary <- aggregate(data=data,change~condition+sex,FUN=summary.function)
> data.summary
  condition sex  change.1  change.2  change.3  change.4
1   aspirin   F  3.420000 -3.420000 -4.500000 -2.200000
2   placebo   F  2.058333 -2.058333 -3.200000 -1.100000
3   aspirin   M  5.411111 -5.411111 -7.100000 -3.300000
4   placebo   M  0.975000 -0.975000 -1.900000  0.000000
> colnames(data.summary) <- c("condition","sex","absmean","mean","min","max")
Error in `colnames<-`(`*tmp*`, value = c("condition", "sex", "absmean",  : 
  'names' attribute [6] must be the same length as the vector [3]

colnames()函数获取错误的列名:

> colnames(data.summary)
[1] "condition" "sex"       "change"

有人能帮助我吗?

编辑: 在尝试其他软件包而不是在r基础之后,它也适用于

library(doBy) data.summary <- summaryBy(change ~ sex + condition, data=data, FUN=summary.function) colnames(data.summary) <- c("condition","sex","absmean","mean","min","max")

0 个答案:

没有答案