how to use melt and dcast on tough data frame

余生颓废 提交于 2019-12-24 14:18:50

问题


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.


回答1:


unnest from tidyr works

library(tidyr)
unnest(dat, Z)


来源:https://stackoverflow.com/questions/31995362/how-to-use-melt-and-dcast-on-tough-data-frame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!