更新时间:2023-12-04 13:40:28
更简单的选择是 separate_rows
library(tidyr)
separate_rows(dat, ID)
# ID keep
#1 E87 1
#2 E42 2
#3 E39 3
#4 E16 4
#5 E17 4
#6 E18 4
#7 E760 5
#8 E761 5
#9 E762 5
或者使用OP的方法,在拆分 ID后,用'保持'列,然后堆叠
到两列data.frame
Or using the OP's method, after splitting the 'ID', name it with 'keep' column and then stack
it to a two column data.frame
stack(setNames(strsplit(dat$ID, ","), dat$keep))