我试图根据分界模式将数据帧中的一列字符串拆分为多个列,即一列字符串" b"和" c d"应分成两列,一列用" a" " c"和另一个" b"和" d"。在我的例子中,分隔模式是任何不包含a-zA-Z0-9@.
根据建议here我尝试使用stringr
。但是,似乎str_split
对空格的数量很敏感?
> xxx<-str_split_fixed(c("a b", "c d")," ",3)
> xxx
[,1] [,2] [,3]
[1,] "a" "b" ""
[2,] "c" "" "d"
> xxx<-str_split_fixed(c("a b", "c d"),"[:space:] ",3)
> xxx
[,1] [,2] [,3]
[1,] "" "b" ""
[2,] "" " d" ""
> xxx<-str_split_fixed(c("a b", "c d"),"' '+",3)
> xxx
[,1] [,2] [,3]
[1,] "a b" "" ""
[2,] "c d" "" ""