In a data frame, I am attempting to duplicate the first occurence of a string into the same column, but also into the neighbouring column. More specifically, I want the first occurence of a string in column v1 to be duplicated and inserted above itself and above the same row in column v2, as exemplified in the mock data frame below:
Input:
df_1<-data.frame("v1"=c(rep("a",times=3),rep("aa",times=4)),"v2"=c(c("b","c","d"),c("bb","cc","dd","ee")))
df_1
v1 v2
1 a b
2 a c
3 a d
4 aa bb
5 aa cc
6 aa dd
7 aa ee
Expected output:
df_2<-data.frame("v1"=c(rep("a",times=4),rep("aa",times=5)),"v2"=c(c("a","b","c","d"),c("aa","bb","cc","dd","ee")))
df_2
v1 v2
1 a a
2 a b
3 a c
4 a d
5 aa aa
6 aa bb
7 aa cc
8 aa dd
9 aa ee
So in this case, the first occurence of "a" and "aa" has been duplicated and inserted into the same data frame above it's first occurence.
I hope my question makes sense.
Best, Rikki