-1

I've got a dataframe like this:

The first column is numeric, and the second column is a comma separated list (character)

id numbers
1 2,4,5
2 1,4,6
3 NA
4 NA
5 5,1,2

And I want to in essence "melt" the dataframe similar to the reshape package. So that the output is a dataframe which looks like this

id numbers
1 2
1 4
1 5
2 1
2 4
2 6
3 NA
4 NA
5 5
5 1
5 2

Except in the reshape2 package each number will have to be each in a column... which takes up too much storage space if there are many numbers... which is why I have opted to set the list of numbers as a comma separated list. But melt no longer works with this setup.

Can you recommend the most efficient way to achieve the transformation from the input dataframe to output dataframe?

HollowBastion
  • 223
  • 3
  • 10
  • 1
    Check some of these Q&A's: http://stackoverflow.com/search?q=%5Br%5D+cSplit+long – talat Sep 26 '16 at 10:27
  • A question like [this](http://stackoverflow.com/questions/30207500/csplit-librarysplitstackshape-is-always-dropping-the-column) will give you some useful information. – jazzurro Sep 26 '16 at 10:27

1 Answers1

-2

The way I would do it for each row, create a data.frame and store them in a list, where df is your initial data.frame.

l = list()
for (j in 1:nrow(df)){
  l[[j]] = data.frame(id      = df$id[[j]],
                      numbers = split(df$numbers[[j]], ','))
}

Afterwards, you can stack all list elements into a single data.frame using plyr::ldply with the 'data.frame' option.

Vlad Calin
  • 11
  • 1