开发者

How to group one column into intervals, and aggregate the corresponding values from another column [closed]

开发者 https://www.devze.com 2023-03-25 20:34 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. 开发者_高级运维 Closed 11 years ago.

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))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号