how to use melt and dcast on tough data frame

2019-08-13 07:22发布

I have a data frame that has one value in each cell, but my last column is a list. Example. Here there are 3 columns. X and Y columns have one value in each row. But column Z is actually a list. It can have multiple values in each cell.

      X Y    Z
    1 a d  h, i, j
    2 b e  j, k
    3 c f  l, m, n, o

I need to create this:

  X Y    Z
 1 a d  h
 2 a d  i
 3 a d  j
 4 b e  j
 4 b e  k
 5 c f  l
 6 c f  m
 7 c f  n
 8 c f  o

Can someone help me figure this out ? I am not sure how to use melt or dcast or any other function for this.

Thanks.

标签: r reshape2 melt
1条回答
看我几分像从前
2楼-- · 2019-08-13 07:53

unnest from tidyr works

library(tidyr)
unnest(dat, Z)
查看更多
登录 后发表回答