Suppose we have a character vector:
foo <- c("A;B;C", "B;C", "F;G;H")
I wonder how to generate the following data frame:
1 A
1 B
1 C
2 B
2 C
3 F
3 G
3 H
Please note that numbers in the first column designate position of the component/element in the character vector foo
.
My initial attempt goes as follows. I use str_split()
function from stringr
package to separate elements in the foo
vector:
> str_split(foo, pattern = ";")
[[1]]
[1] "A" "B" "C"
[[2]]
[1] "B" "C"
[[3]]
[1] "F" "G" "H"
Now, I guess, the best idea is to use some sort of map
function from the purrr
package, but unfortunately I have no idea how to proceed.