1

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.

Jaap
  • 81,064
  • 34
  • 182
  • 193
user2904120
  • 416
  • 1
  • 4
  • 18

1 Answers1

5

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
Pierre L
  • 28,203
  • 6
  • 47
  • 69