One idea is to replace the first _
with another delimiter and split on the new delimiter. This works because using sub
will only replace the first found delimiter (whereas gsub
replaces all), i.e.
strsplit(sub('_', ',', x$a), ',', fixed = TRUE)
#[[1]]
#[1] "A" "B_D"
#[[2]]
#[1] "B" "C"
To create two new columns in your original data frame,
within(x, new <- data.frame(do.call(rbind, strsplit(sub('_', ',', x$a), ',', fixed = TRUE))))
# a new.X1 new.X2
#1 A_B_D A B_D
#2 B_C B C
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…