I have a data frame with a column of IDs and a column of concatonated strings with 1-3 elements each. I need to separate string field and unpivot the result such that I'm left with two columns, one with non-unique IDs, and one with strings that have one element each. Here's some example data:
startdata <- data.frame(IDs = c(1,2,3), TextField = c("abc", "def & ghi", "jkl & mno & pqr"), stringsAsFactors = FALSE)
So it starts out looking like this:
IDs TextField
1 1 abc
2 2 def & ghi
3 3 jkl & mno & pqr
In the end, I need it to look like this:
IDs TextField
1 1 abc
2 2 def
3 2 ghi
4 3 jkl
5 3 mno
6 3 pqr
I can use strsplit to separate them out into a list like this:
> strsplit(startdata$TextField, " & ")
[[1]]
[1] "abc"
[[2]]
[1] "def" "ghi"
[[3]]
[1] "jkl" "mno" "pqr"
But I get stuck there.
I try to stick to the default libraries as much as possible, but feel free to let me know if this is accomplished much better by a different library.