0

I have a dataset that contains one variable which is a list. I would now like to reshape the whole dataset with respect to the values in that list.

test_frame = data.frame(var1 = c("AAA", "BBB"))
test_frame$var2 = list(c("1", "2"), c("4", "5", "6"))

That data.frame looks like this:

  var1    var2
1  AAA    1, 2
2  BBB 4, 5, 6

I would like to get something like this:

  var1    var2
1  AAA    1
2  AAA    2
3  BBB    4
4  BBB    5
5  BBB    6

My idea was to create variables for the maximum number of entries in var2, then assign the different elements to those variables (leaving the rest as NA), then reshape and then filter out all NAs. That seems quite inefficient and not very elegant though. Any other/more efficient solutions?

tho_mi
  • 698
  • 3
  • 9
  • 16
  • Since this is already a list and not a string we can load `library(dplyr)` and `library(tidyr)` and then simply use ` test_frame %>% unnest(var2)` (see also second answer in linked duplicate post) – dario Mar 10 '20 at 16:08
  • Works perfectly, thanks! – tho_mi Mar 10 '20 at 16:22

0 Answers0