Question:
I have a data.frame with a column with strings like "123-235-203".
For example, the line:
string column1 column2
123-235-203 xy
I want to separate this string, so that the line that contains it is multiplied, preserving the values of the other columns:
strsplit(string, "-")
Turning the line into 3 lines.
string column1 column2
123 xy
235 xy
203 xy
Is there a way to do this using dplyr or tidyr?
Answer:
Hadley himself offers us the answer in the tidyr vignette.
library(tidyr)
library(dplyr)
df <- data.frame(
x = 1:3,
y = c("a", "d,e,f", "g,h"),
stringsAsFactors = FALSE
)
df %>%
transform(y = strsplit(y, ",")) %>%
unnest(y)