I'm working with a string that is a list of elements separated by commas. I want to separate the string so that each element has its column. But I'm having trouble because there are a different number of elements per list.
X1 <- "a,b,c"
X2 <- "a,b"
X3 <- "a,b,c,d"
DF <- data.frame(Col1= rbind(X1,X2,X3))
Col1
X1 a,b,c
X2 a,b
X3 a,b,c,d
I would like it to look like this. Where each element has its own column, irrespective of how many elements there are in the character's string.
Col1 Col2 Col3 Col4
X1 a b c
X2 a b
X3 a b c d
Thank You!
We could use cSplit
from splitstackshape
library(splitstackshape)
cSplit(DF, "Col1",",")
-output
cSplit(DF, "Col1",",")
Col1_1 Col1_2 Col1_3 Col1_4
1: a b c <NA>
2: a b <NA> <NA>
3: a b c d