Can someone help me get the difference between aggregate and ddply with the following example:
A data frame:
mydat <- data.frame(first = rpois(10,10), second = rpois(10,10),
third = rpois(10,10), group = c(rep("a",5),rep("b",5)))
Use aggregate to apply a function to a part of the data frame split by a factor:
aggregate(mydat[,1:3], by=list(mydat$group), mean)
Group.1 first second third
1 a 8.8 8.8 10.2
2 b 6.8 9.4 13.4
Try to use aggregate for another function (returns an error message):
aggregate(mydat[,1:3], by=list(mydat$group), function(u) cor(u$first,u$second))
Error in u$second : $ operator is invalid for atomic vectors
Now, try the same with ddply (plyr package):
ddply(mydat, .(group), function(u) cor(u$first,u$second))
group V1
1 a -0.5083042
2 b -0.6329968
All tips, links, criticism are highly appreciated.