I have a data frame with four columns: user_id, event, and time
User_id a user_id, event is either "A" or "B", and time is time. I need to count the number of "B" values that occur before each "A" value. So if there are 3 "B" values that occur before the first "A" then that instance of "A" will get a new column with a value of 3. If there are 25 instances of "B" before the next values of "A" then that will get a value of 25. I consider myself a solid R/dplyr journeyman but this has me stumped! Thanks.
user_id event date_time desired_column
1 B 2018-01-01 NA
1 B 2018-01-02 NA
1 B 2018-01-03 NA
1 B 2018-01-04 NA
1 B 2018-01-05 NA
1 A 2018-01-06 5
1 B 2018-01-07 NA
1 A 2018-01-08 1
2 B 2018-01-05 NA
2 B 2018-01-06 NA
2 A 2018-01-07 2
2 B ... NA
2 A ... 1
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…