Reputation: 1628
I've found similar queries listed on here, but none of them have been able to work for me. I have binary data listed in a data frame which I want to aggregate according to another variable. For example.
Data.frame (A & B are columns)
A B
1 23
0 7
0 23
0 7
1 4
I've tried the below (which worked when finding the mean) and get the following error message:
aggregate( A~B, data.frame, sum)
Error in FUN(X[[1L]], ...) : invalid 'type' (character) of argument
Ideally I would like an output which gives 23 = 1, 7 = 0, 4 = 1
Can anyone help me please?
Thanks in advance!
Upvotes: 0
Views: 8357
Reputation: 7123
What did you call exactly? What is the str
of your data.frame
?
mdf <- data.frame( A = c(1,0,0,0,1), B = c(23, 7 ,23, 7,4)
aggregate( A ~ B, mdf, sum )
gives
B A
1 4 1
2 7 0
3 23 1
EDIT:
So just in case your problem is, that your column A
is not numeric you can fix this by
mdf$A <- as.numeric( as.character( mdf$A ) )
Upvotes: 0
Reputation: 8017
Many ways to do this, but for a start:
library(plyr)
foo <- data.frame(A = c(1, 0, 0, 0, 1),
B = c(23, 7, 23, 7, 4))
ddply(foo, .(B), summarise, sum = sum(A))
gives:
> ddply(foo, .(B), summarise, sum = sum(A))
B sum
1 4 1
2 7 0
3 23 1
>
Upvotes: 1