在 dplyr 中有条件地计数 [英] Conditionally Count in dplyr

查看:30
本文介绍了在 dplyr 中有条件地计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些会员订单数据,我想按订单周汇总.

I have some member order data that I would like to aggregate by week of order.

这是数据的样子:

memberorders=data.frame(MemID=c('A','A','B','B','B','C','C','D'),
             week = c(1,2,1,4,5,1,4,1),
             value = c(10,20,10,10,2,5,30,3))

我正在使用 dplyr to group_by MemID 并总结值";对于 week<=2week<=4(查看每个成员在第 1-2 周和第 1-4 周订购了多少.我目前拥有的代码是:

I'm using dplyr to group_by MemID and summarize "value" for week<=2 and week<=4 (to see how much each member ordered in weeks 1-2 and 1-4. The code I currently have is:

MemberLTV <- memberorders %>%
group_by(MemID) %>%
summarize(
sum2 = sum(value[week<=2]),
sum4 = sum(value[week<=4]))

我现在尝试在汇总中再添加两个字段,count2 和 count4,这将计算每个条件的实例数(week <=2week <;=4).

I'm now trying to add two more fields in summarize, count2 and count4, that would count the number of instances of each condition (week <=2 and week <=4).

所需的输出是:

output  = data.frame(MemID = c('A','B','C','D'),
                 sum2 = c(30,10,5,3),
                 sum4 = c(30,20,35,3),
                 count2 = c(2,1,1,1),
                 count4 = c(2,2,2,1))

我猜这只是对 sum 函数的一点点调整,但我无法弄清楚.

I'm guessing it's just a little tweak of the sum function but I'm having trouble figuring it out.

推荐答案

尝试

 library(dplyr)
 memberorders %>% 
        group_by(MemID) %>% 
        summarise(sum2= sum(value[week<=2]), sum4= sum(value[week <=4]), 
                  count2=sum(week<=2), count4= sum(week<=4))

这篇关于在 dplyr 中有条件地计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆