How to split strings into new rows while maintaining other columns in R [duplicate]

强颜欢笑 提交于 2020-02-04 02:13:12

问题


I am wanting to split a character vector column into multiple rows (of the same dataframe), while maintaining other columns (keep) in this reproducible example:

dat<-structure(list(ID = c("E87", "E42", "E39", "E16,E17,E18", "E760,E761,E762"), keep = 1:5), row.names = c(NA, 5L), class = "data.frame")
> dat
              ID keep
1            E87    1
2            E42    2
3            E39    3
4    E16,E17,E18    4
5 E760,E761,E762    5

Of course we can split ID with strsplit, but the output is in list format (which is always confusing to me for some reason), and without the column keep

strsplit(dat$ID, ",")

[[1]]
[1] "E87"

[[2]]
[1] "E42"

[[3]]
[1] "E39"

[[4]]
[1] "E16"  " E17" " E18"

[[5]]
[1] "E760" "E761" "E762"

Using unlist I can get this output back into a vector, but now the order will surely be lost to be able to recombine keep with ID.

unlist(strsplit(dat$ID, ","))

[1] "E87"  "E42"  "E39"  "E16"  " E17" " E18" "E760" "E761" "E762"

Any thoughts as to how I might get this output:

> dat
              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

回答1:


An easier option is 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

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))


来源:https://stackoverflow.com/questions/59147988/how-to-split-strings-into-new-rows-while-maintaining-other-columns-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!