Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

r - Conditionally Count in dplyr

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...