Dear R experts,
I would like to collapse rows if a condition repeats in the following rows. For example, the original data is as follows
dataset <- data.frame(id = c("a",'a','a','a', "b", "b",'b','b'),
id2 = c("1","2","2","2", "1","1","2",'1'),
text = c("gi", "hi",'ed','ew', "ic",'ia',"ws",'er'),
stringsAsFactors = FALSE)
for id=='a', variable id2 repeats consecutively for three times (2,2,2);
for id=='b', variable id2 repeats consecutively for two times(1,1);
I would like to merge the text variable if id2 repeated in the following rows and results in dataset2.
dataset2 <- data.frame(id = c("a",'a', "b", "b",'b'),
id2 = c("1", "2", "1","2",'1'),
text = c("gi", "hi ed ew", "ic ia","ws",'er'),
stringsAsFactors = FALSE)
Do you have suggestion to achieve it? Thank you very much.
Best wishes,
Veda