0

Suppose the following data:

foo <- data.frame(
  key=c('one', 'two', 'three'), val=c('a', 'b|c', 'd|e|f'),
  stringsAsFactors = F)

It looks like

> foo
    key   val
1   one     a
2   two   b|c
3 three d|e|f

I want output that is as follows:

bar <- data.frame(key=c('one', 'two', 'two', 'three', 'three', 'three'),
                  val=c('a', 'b', 'c', 'd', 'e', 'f'),
                  stringsAsFactors = F)

That looks like

> bar
    key val
1   one   a
2   two   b
3   two   c
4 three   d
5 three   e
6 three   f

The psuedo-code might be: split val by pipe (|), but into a variable (unknown) number of columns, then pivot longer.

Suggestions?

Ideally using the tidyverse.

dfrankow
  • 20,191
  • 41
  • 152
  • 214

2 Answers2

0

Found the answer in the question I linked. It will look like this:

bar <- foo %>% mutate(val=strsplit(val, "\\|")) %>% unnest(val)
dfrankow
  • 20,191
  • 41
  • 152
  • 214
0

You could nest and unnest, but actually it works without using summarize():

foo %>% 
  group_by(key) %>% 
  summarize(val = unlist(str_split(val, pattern = "[|]"))) %>% 
  ungroup()
dufei
  • 2,166
  • 1
  • 7
  • 18