This question already has answers here:
R strsplit with multiple unordered split arguments?

(4个答案)


去年关闭。





我该如何分割

 Chr3:153922357-153944632(-)
 Chr11:70010183-70015411(-)


进入

    Chr3  153922357 153944632 -
    Chr11 70010183  70015411  -


我尝试了strsplit(df$V1,"[[:punct:]]")),但最终结果中没有出现负号

最佳答案

您也可以从str_split尝试stringr

library(stringr)
lapply(str_split(df$V1, "(?<!\\()\\-|[:\\)\\(]"), function(x) x[x != ""])


结果:

[[1]]
[1] "Chr3"      "153922357" "153944632" "-"

[[2]]
[1] "Chr11"    "70010183" "70015411" "-"


数据:

df = read.table(text = " Chr3:153922357-153944632(-)
 Chr11:70010183-70015411(-) ")

08-25 06:13