1

First of all I'm not sure if I titled this topic right but I hope description will clear everything.

Firstly, I had data frame like this

vehicle type                    number1 number2
"car"   "truck, SUV, hatchback" 2       3
"bike"  "scraper, lowrider"     5       7
"plane" "bomber"                2       3

Secondly, I've used great cSplitfunction from splitstackshape to change this data frame to something like this

vehicle type1     type2      type3       number1 number2
"car"   "truck"   "SUV"      "hatchback" 2       3
"bike"  "scraper" "lowrider" NA          5       7
"plane" "bomber"  NA         NA          2       3

But as my final result I would like to see something like this

vehicle type        number1 number2
"car"   "truck"     2       3
"car"   "SUV"       2       3
"car"   "hatchback" 2       3 
"bike"  "scraper"   5       7
"bike"  "lowrider"  5       7
"plane" "bomber"    2       3

And obviously I have no idea how to do this and I don't even know if topic describes my problem enough (I have feeling that package tidy may be helpful). Thank you in advance

AlienDeg
  • 1,288
  • 1
  • 13
  • 23

1 Answers1

3

The default direction is wide in cSplit. We can specify the direction='long' to get the expected output.

library(splitstackshape)
cSplit(df1, "type", sep=", ", "long")
#    vehicle      type number1 number2
#1:     car     truck       2       3
#2:     car       SUV       2       3
#3:     car hatchback       2       3
#4:    bike   scraper       5       7
#5:    bike  lowrider       5       7
#6:   plane    bomber       2       3
akrun
  • 874,273
  • 37
  • 540
  • 662