expand delimited columns in R [duplicate]

ⅰ亾dé卋堺 提交于 2020-01-25 03:55:07

问题


I'd like to expand or split a large data frame with multiple columns according to values in the first column:

Here is my input:

a1;a2;a3    X   1
b1;b2       Y   2
c           Z   3
d1;d2;d3    ZZ  4

and output:

a1  X   1
a2  X   1
a3  X   1
b1  Y   2
b2  Y   2
c   Z   3
d1  ZZ  4
d2  ZZ  4
d3  ZZ  4

So far I came across the following solution - http://www.r-bloggers.com/expand-delimited-columns-in-r/ but I hope someone could suggest a more straightforward approach.

I'd appreciate any help.


回答1:


Try cSplit, it has an argument to split long instead of wide:

library(splitstackshape)
cSplit(mydata, "V1", direction="long", sep=";")
#    V1 V2 V3
# 1: a1  X  1
# 2: a2  X  1
# 3: a3  X  1
# 4: b1  Y  2
# 5: b2  Y  2
# 6:  c  Z  3
# 7: d1 ZZ  4
# 8: d2 ZZ  4
# 9: d3 ZZ  4


来源:https://stackoverflow.com/questions/34725964/expand-delimited-columns-in-r

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