-1

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.

Andrej
  • 3,554
  • 10
  • 38
  • 69

1 Answers1

0

One option is separate_rows after creating a data.frame with index as the sequence of 'foo'

library(tidyverse)
data.frame(ind = seq_along(foo), Col = foo) %>%
    separate_rows(Col)

Or using the OP's method, split the 'foo' into a list, set the names with the sequence of 'foo' and stack to a two column data.frame'

stack(setNames(str_split(foo, pattern = ";"), seq_along(foo)))
akrun
  • 789,025
  • 32
  • 460
  • 575