3 ms·
Thank you- that's a terrific writeup. I had never put it together that the a main functional difference between tapply and plyr is that you can group by multip
by bernardom 13y ago
Thank you- that's a terrific writeup.
I had never put it together that the a main functional difference between tapply and plyr is that you can group by multiple columns.
One thing that I've had to use plyr for:
dfrm has three columns: user_id, vals, date
I want to know the average val for the first day of each user_id, second day, and so on. (In other words, average of vals across users relativized to the rank of the date for that user).
This is when plyr is awesome:
dfrm <- ddply(dfrm,"user_id",transform,dateRank = order(date))
That one line split my dataframe by user_id, performed a function over every sub-dfrm (ranking by date), then combined them back together into one dfrm, with a new column names dateRank that is appopriate for that {user,date} pair.
Now it's easy to:
summaryTable <- data.frame(mean=numeric(), median=numeric, n=numeric())
summaryTable$mean <- tapply(dfrm$val, dfrm$dateRank, mean)
summaryTable$median <- tapply(dfrm$val, dfrm$dateRank, median)
summaryTable$n <- tapply(dfrm$val, dfrm$dateRank, length)
And so on.