使用R中的自己的函数汇总数据框中的任意列数

时间:2015-01-04 21:50:26

标签: r plyr dplyr

我正在寻找一种方法来总结R中实验结果的大平面表。由于我需要总结任意列数(不能事先对列进行硬编码),因此摘要直截了当并使用任意定义的摘要函数。

举个例子说我有以下平面表my_table

my_table
   id_1 id_2 rep_id value_1 value_2
1     a    1      1     0.0     0.0
2     a    1      2     0.2     0.2
3     a    1      3     0.3     0.3
4     a    1      4     0.4     0.4
5     a    1      5     0.1     0.1
6     a    2      1     0.5     0.0
7     a    2      2     1.5     1.5
8     a    2      3     2.5     2.5
9     a    2      4     3.5     3.5
10    a    2      5     4.5     4.5

我会将my_table汇总到一个表格中,例如:

> summary_table
  id_1 id_2 value_1.min value_1.max value_1.mean_plus_sd value_2.min value_2.max value_2.mean_plus_sd
1    a    1         0.0         0.4            0.3581139           0         0.4            0.3581139
2    a    2         0.5         4.5            4.0811388           0         4.5            4.1464249

摘要很复杂,因为我想:

  1. 指定要分组的变量,例如key_fields = c("id_1","id_2")
  2. 指定要汇总的列,例如fields_to_summarize = c("value_1","value_2")
  3. 使用我自己的汇总函数(也命名新列)
  4. 以下是我目前用于执行所有这三项操作的代码。这很好,但它也非常低效。任何改进都会非常感激:

    library(plyr)
    
    # create table
    my_table = data.frame("id_1"  = c("a","a","a","a","a","a","a","a","a","a")
                        ,"id_2" = c("1","1","1","1","1","2","2","2","2","2")
                        ,"rep_id" = c(1,2,3,4,5,1,2,3,4,5)
                        ,"value_1"= c(0.0,0.2,0.3,0.4,0.1,0.5,1.5,2.5,3.5,4.5)
                        ,"value_2"= c(0.0,0.2,0.3,0.4,0.1,0.0,1.5,2.5,3.5,4.5)
        )
    
    # specify columns to group by / summarize over
    key_fields = c("id_1","id_2")
    fields_to_summarize = c("value_1","value_2")
    
    # create summary_table
    counter = 1;
    for (fname in fields_to_summarize){
    
      summary_function = function(D) data.frame(setNames(list(min(D[[fname]]),
                                                              max(D[[fname]]),
                                                              mean(D[[fname]])+sd(D[[fname]])),
                                                         paste(fname,c("min",
                                                                       "max",
                                                                       "mean_plus_sd"),
                                                               sep=".")
      ))
    
      tmp = ddply(.data = df, 
                     .variable = key_fields,
                     function(D) summary_function(D))
    
      if (counter == 1){
        summary_table = tmp;
      } else {
        summary_table = join(x=summary_table,y=tmp,by=key_fields,type="left", match="all")
      }
      counter = counter + 1;
    }
    

2 个答案:

答案 0 :(得分:4)

不是最终的解决方案,但也许是dplyr

的良好开端
library(dplyr)

mean_plus_sd <- function(x) mean(x) + sd(x)
key_fields = c("id_1","id_2")

my_table %>%
  group_by_(.dots = key_fields) %>%
  summarise_each_(funs(min,max,mean_plus_sd), fields_to_summarize)

答案 1 :(得分:2)

这里有两个你可以定义的快速功能。首先是使用基础R方法,其次是使用可能的data.table方法

My_func <- function(data, fields_to_summarize, key_fields){
                    aggregate(data[fields_to_summarize], 
                     data[key_fields], 
                     function(x) c(min = min(x), 
                                  max = max(x),
                                  mean_plus_sd = mean(x) + sd(x)))
}

My_func2 <- function(data, fields_to_summarize, key_fields){
                as.data.table(data)[, lapply(.SD, 
                                      function(x) c(min(x), max(x), mean(x) + sd(x))), 
                key_fields, 
                .SDcols = fields_to_summarize][, 
                      Funs := c("min", "max", "mean_plus_sd")][]
}

测试第一个功能

key_fields = c("id_1","id_2")
fields_to_summarize = c("value_1","value_2")

My_func(my_table, fields_to_summarize, key_fields)
#   id_1 id_2 value_1.min value_1.max value_1.mean_plus_sd value_2.min value_2.max value_2.mean_plus_sd
# 1    a    1   0.0000000   0.4000000            0.3581139   0.0000000   0.4000000            0.3581139
# 2    a    2   0.5000000   4.5000000            4.0811388   0.0000000   4.5000000            4.1464249

测试第二个功能

library(data.table)
My_func2(my_table, fields_to_summarize, key_fields)

#    id_1 id_2   value_1   value_2         Funs
# 1:    a    1 0.0000000 0.0000000          min
# 2:    a    1 0.4000000 0.4000000          max
# 3:    a    1 0.3581139 0.3581139 mean_plus_sd
# 4:    a    2 0.5000000 0.0000000          min
# 5:    a    2 4.5000000 4.5000000          max
# 6:    a    2 4.0811388 4.1464249 mean_plus_sd