2

I have some nested data within a DF and I would like to expand it to a new DF with all other entries duplicated. For example

    x1 x2       x3
1   43 'j,k,d'  'apple'
2   11 'as,ddd' 'pear'

In this DF the variable x2 has multiple entries separated by comma. I would like to produce a new DF that looks the following:

    x1 x2       x3
1   43 'j'     'apple'
2   43 'k'     'apple'
3   43 'd'     'apple'
4   11 'as'    'pear'
5   11 'ddd'   'pear'
Francis Smart
  • 3,875
  • 6
  • 32
  • 58

1 Answers1

4

Try

library(splitstackshape)
cSplit(df1, 'x2', ',', 'long')
#   x1  x2    x3
#1: 43   j apple
#2: 43   k apple
#3: 43   d apple
#4: 11  as  pear
#5: 11 ddd  pear
akrun
  • 874,273
  • 37
  • 540
  • 662