1

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.

Frameworker247
  • 117
  • 1
  • 9
  • http://stackoverflow.com/questions/15347282/split-delimited-strings-in-a-column-and-insert-as-new-rows or http://stackoverflow.com/questions/13773770/split-comma-separated-column-into-separate-rows as a duplicate to this question. You don't have a comma separating, but the logic should be identical. – thelatemail Feb 20 '17 at 22:47

0 Answers0