问题
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