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.