Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Rolling Wall Count Variable in R

Tags:

r

count

have a dataset with around 21k in observations, and a categorical variable for each observation with options A, B and C. I'm looking to create an experience variable for countries that have previously taken option C in prior observations (case t-1 to put it simpler). I've been told this is called a rolling wall count. I haven't been able to figure out how to go about this or what package is best to use. Any suggestions would be super helpful!

dispute=c("1","1","1","2","2","2","2","3","3","3")
partner=c("1","2","3","1","2","3","4","2","1","3")
position=c("A","C","C","B","C","A","C","B","C","C")

Currently my data looks something like this:

Dispute Partner Position
1        1       A
1        2       C
1        3       C
2        1       B
2        2       C
2        3       A
2        4       C
3        1       B
3        2       C
3        3       C

Ideally I create a variable that cumulatively counts when each unique observation takes on the value C (generating an "experience" count for each unique "partner"

Dispute Partner Position Experience
1        1       A       NA
1        2       C       1
1        3       C       1
2        1       B       NA
2        2       C       2
2        3       A       NA
2        4       C       1
3        1       B       NA
3        2       C       3
like image 429
Carefreewritingsonthewall Avatar asked Jun 04 '26 12:06

Carefreewritingsonthewall


1 Answers

With data.table

library(data.table)
setDT(df)[, experience:=cumsum(position=="C")*(position=="C"), by=partner] 

    dispute partner position experience
 1:       1       1        A          0
 2:       1       2        C          1
 3:       1       3        C          1
 4:       2       1        B          0
 5:       2       2        C          2
 6:       2       3        A          0
 7:       2       4        C          1
 8:       3       2        B          0
 9:       3       1        C          1
10:       3       3        C          2   

With dplyr

library(dplyr)
df %>% 
  group_by(partner) %>% 
  mutate(experience=cumsum(position=="C")*(position=="C"))

   dispute partner position experience
1        1       1        A          0
2        1       2        C          1
3        1       3        C          1
4        2       1        B          0
5        2       2        C          2
6        2       3        A          0
7        2       4        C          1
8        3       2        B          0
9        3       1        C          1
10       3       3        C          2

data

df <- data.frame(dispute=c("1","1","1","2","2","2","2","3","3","3"),
                     partner=c("1","2","3","1","2","3","4","2","1","3"),
                     position=c("A","C","C","B","C","A","C","B","C","C"))
like image 52
ExperimenteR Avatar answered Jun 06 '26 05:06

ExperimenteR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!