Using reshape + cast to aggregate over multiple columns
- by DamonJW
In R, I have a data frame with columns for Seat (factor), Party (factor) and Votes (numeric). I want to create a summary data frame with columns for Seat, Winning party, and Vote share. For example, from the data frame
df <- data.frame(party=rep(c('Lab','C','LD'),times=4),
votes=c(1,12,2,11,3,10,4,9,5,8,6,15),
seat=rep(c('A','B','C','D'),each=3))
I want to get the output
seat winner voteshare
1 A C 0.8000000
2 B Lab 0.4583333
3 C C 0.5000000
4 D LD 0.5172414
I can figure out how to achieve this. But I'm sure there must be a better way, probably a cunning one-liner using Hadley Wickham's reshape package. Any suggestions?
For what it's worth, my solution uses a function from my package
djwutils_2.10.zip and is invoked as follows. But there are all sorts of special cases it doesn't deal with, so I'd rather rely on someone else's code.
aggregateList(df, by=list(seat=seat),
FUN=list(winner=function(x) x$party[which.max(x$votes)],
voteshare=function(x) max(x$votes)/sum(x$votes)))