In a datafreame i have 2 variables, one for number of Free Samples sent, and the other for Number of Purchases resulted. I would like to group free sample variables into intervals of say 0, 1 to 5, 5 to 10, more than 10. Then cumulate the observations from the number of purchases column withing each of the intervals to present as a table.
Any help would be greatly appreciated
In base R the way to do it is straightforward. First generate your new variable and then use ave()
binnedSamples <- cut( myDF$freeSamples, breaks = c(0, 1, 5, 10, 10^6) )
tapply( myDF$purchases, binnedSamples, sum )
(start accepting answers and voting ones you like up as well)
Here is one way using the plyr
library
require(plyr)
mydf = data.frame(
npurchases = rpois(20, 10),
nsamples = rpois(20, 10)
)
ddply(mydf, .(cut(nsamples, breaks = c(0, 1, 5, 10, 10^6))), summarize,
npurchases = sum(npurchases))
精彩评论