使用ddply基于列二进制数据的合并行[重复](Merging rows of binary dat

2019-10-20 21:18发布

This question already has an answer here:

  • Aggregate / summarize multiple variables per group (e.g. sum, mean) 6 answers

I have the following dataframe for which I want merge together binary values from an amount of rows.

df =data.frame(ID=c(rep("A",5),rep("B",5)), nr=c(rep("2",5),rep("3",5)), replicate(10,sample(0:1,10,rep=TRUE)))

eg:

# ID nr X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
# A  2  0  0  1  1  1  1  1  1  1   0
# A  2  1  0  0  0  0  0  0  1  0   1
# A  2  0  0  1  1  1  0  0  0  0   1
# A  2  0  0  0  0  0  1  1  1  0   1
# A  2  0  0  0  1  0  1  1  0  1   1
# B  3  0  1  0  0  1  0  0  0  1   1
# B  3  1  1  0  0  0  0  0  0  0   1
# B  3  1  0  1  0  0  0  1  1  0   1
# B  3  1  1  1  0  1  0  0  1  1   1
# B  3  0  0  0  1  0  0  0  1  0   1

Now I want to merge rows for the first 2 columns in this case:

df2 = ddply(df, c(1:2), summarise, numcolwise(sum,c(3:12)))

But I get the following error:

Error in vector(type, length) : 
   vector: cannot make a vector of mode 'closure'.

Also I would want that anything higher than 1 to be reset to 1 to keep it binary, but since I couldn't get past the error I haven't tried it yet.

I know variations of this question have been asked before but I haven't found it like this before. Keep in mind that I want to use column indices because I'm working with large data.

Answer 1:

如果你的数据是相当大的(如在评论中提到的),忘了plyr ,尝试data.table

library(data.table)
setDT(df)[, lapply(.SD, sum), by = list(ID, nr)]

##    ID nr X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
## 1:  A  2  2  3  5  2  5  2  1  3  4   1
## 2:  B  3  3  3  4  1  3  2  3  2  1   4

或者,如果你要坚持与plyr家庭,移动到下一代: dplyr

library(dplyr)
df %>%
  group_by(ID, nr) %>%
  summarise_each(funs(sum))

# Source: local data table [2 x 12]
# Groups: ID
# 
#   ID nr X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
# 1  A  2  2  3  5  2  5  2  1  3  4   1
# 2  B  3  3  3  4  1  3  2  3  2  1   4


文章来源: Merging rows of binary data based on columns using ddply [duplicate]
标签: r sum plyr