R在大量列上聚合而不指定列名

Luc*_*Luc 2 aggregate r dataframe

我无法使用此处的搜索功能或Google上的问题找到问题的答案.

我有一个数据框(500列宽,200,000行长),每人多行.每个单元格(除了具有人员ID的第一列)包含0或1.我正在寻找一种方法将每个人的数据帧减少到1行,其中我按人数取每列的最大值.

我知道我可以使用ddply或data.table ......如下所示......

tt <-data.frame(person=c(1,1,1,2,2,2,3,3,3), col1=c(0,0,1,1,1,0,0,0,0),col2=c(1, 1, 0, 0, 0, 0, 1 ,0 ,1))

library(plyr)
ddply(tt, .(person), summarize, col1=max(col1), col2=max(col2))

  person col1 col2
      1    1    1
      2    1    0
      3    0    1
Run Code Online (Sandbox Code Playgroud)

但我不想指定我的每个列名,因为1)我有500和2)在新数据集上它们可能不同.

MrF*_*ick 5

使用summarise_each来自的功能dplyr

library(dplyr)
tt %>% group_by(person) %>% summarise_each(funs(max))

#   person col1 col2
# 1      1    1    1
# 2      2    1    0
# 3      3    0    1
Run Code Online (Sandbox Code Playgroud)

或只是基本aggregate功能

aggregate(.~person, tt, max)

#   person col1 col2
# 1      1    1    1
# 2      2    1    0
# 3      3    0    1
Run Code Online (Sandbox Code Playgroud)