My data looks like
a
a >> b
d >> c >> e
(unknown times ">>") can be present in a row
And I need them in several columns depending on occurrences of ">>"
I tried strsplit, which converts the column in a list and when I tried to unlist it, it repeats the values like
X1 X2 X3
a a a
a b a
d c e
Is there any Base R solution to get
X1 X2 X3
a
a b
d c e
And so on? I can only use Base R!!
Assuming your input is a character vector as below, try this:
x = c("a" ,
"a >> b" ,
"d >> c >> e")
s = lapply(strsplit(x, split = ">>"), trimws)
s = t(sapply(s, function(x) {length(x) <- max(lengths(s)); x}))
s
# [,1] [,2] [,3]
# [1,] "a" NA NA
# [2,] "a" "b" NA
# [3,] "d" "c" "e"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments