r - collapse text by 2 ID's in a row -
i have question similar topic: "collapse text group in data frame [duplicate]"
group text a1 a2 a3 b b1 b b2 c c1 c c2 c c3 c c4
i collapse 2 sequential id's (not whole id group)
group text a1a2 a2a3 b b1b2 c c1c2 c c2c3 c c3c4
alternative tidyverse
answer:
library(tidyverse) dat %>% group_by(group) %>% mutate(text=paste0(lag(text),text)) %>% slice(-1)
using data.table
:
library(data.table) setdt(dat) dat[, paste0(shift(text,1), text)[-1], by=group] # group v1 #1: a1a2 #2: a2a3 #3: b b1b2 #4: c c1c2 #5: c c2c3 #6: c c3c4
Comments
Post a Comment