I'm trying to split a column of strings in a dataframe into multiple columns based a delimiting pattern, i.e., a column of strings "a b" and "c d" should be split into two columns, one with "a" and "c", and another with "b" and "d". The delimiting pattern is in my case any sequence of characters that does not contain a-zA-Z0-9@.
As suggested here I'm trying to use stringr
. However, it seems that str_split
is sensitive to the number of spaces?
> 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" "" ""