I have a dataset with three columns. The column user has two actions including action1 and action2. action2 only contains information if action1 column has A data. I want to concatenate P data in action1 with the previous data in action2. For example, if action2 has ac, and the next row has P in action1, I want to change P into Pac, and will continue (all P turn into Pac) until action2 change. Take note that this process should repeat for each user.
df<-read.table(text="
user action1 action2
1 A a
1 B NA
1 P NA
1 P NA
1 A ac
1 P NA
2 B NA
2 P NA
2 A aa
2 P NA
2 AB aa",header=T)
result: (I highlighted those rows that infected)
user action1 action2
1 A a
1 B NA
1 Pa NA <-
1 Pa NA <-
1 A ac
1 Pac NA <-
2 B NA
2 P NA
2 A aa
2 Paa NA <-
2 AB NA
Thank you