有条件地在dplyr中计数

编程入门 行业动态 更新时间:2024-10-28 08:18:02
本文介绍了有条件地在dplyr中计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是数据的样子:

memberorders = data.frame(MemID = c('A','A','B','B','B' ,'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_byMemID和总结周的值< = 2和< = 4(以查看在1-2周和1-4周内订购的成员数量。我目前的代码是:

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

我现在正在尝试在总结count2和count4中添加两个字段,这些字段可以计算每个条件的实例数(周< = 2和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))

我猜这只是一个调和功能的一点调整,

解决方案

尝试

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

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

This is what the data looks like:

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

I'm using dplyr to group_by "MemID" and summarize "value" for "week" <=2 and <=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]))

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

The desired output is:

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

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

解决方案

Try

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中计数

本文发布于:2023-11-22 04:10:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有条件   dplyr

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!