I have a df with patient ids and a binary indicator for whether they have experienced an intervention or not. I want to make a new column called "time_post" which tells me how many timepoints have passed since experiencing the intervention.
here is my DF:
names<-c("tom","tom","tom","tom","tom","tom","tom","tom", "john", "john","john", "john","john", "john","john", "john")
post<-as.numeric(0,0,0,1,1,1,1,1,0,1,1,1,1,1,1,1)
df<-data.frame(names,post)
this is what I have tried:
df$time_post<-ifelse(df$post==1[1],1,0) ##this tries to assign 1 to "time_post" for first value of 1 seen in post
df$time_post<-ifelse(df$post==1[2],2,df$time_post) ##trying to apply same logic here, but doesn't work. Introduces NAs into time_post column.
This is my desired output;
names post time_post
1 tom 0 0
2 tom 0 0
3 tom 0 0
4 tom 1 1
5 tom 1 2
6 tom 1 3
7 tom 1 4
8 tom 1 5
9 john 0 0
10 john 1 1
11 john 1 2
12 john 1 3
13 john 1 4
14 john 1 5
15 john 1 6
16 john 1 7
Thank you in advance