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.