0

Suppose I have the following data table:

DT <- data.table(c("A","B"), c("a;a;b","c"))

which outputs:

   V1    V2
   1:  A a;a;b
   2:  B     c

I am trying (without using for loops) to create an output using data table methods that looks like:

       V1    V2
   1:  A     a
   2:  A     a 
   3:  A     b
   4:  B     c

I've been able to do something similar by counting the number of elements separated by ";" and then duplicating rows. From there, I loop to change the values but this seems quite slow and I think I'm missing something obvious.

Thanks!

  • There's `splitstackshape::cSplit(DT, "V2", ";", direction="long")`. Looking to see if there's a matching dupe (previous question) – Frank Apr 26 '18 at 18:46
  • 1
    The answer I posted was `DT[, setNames(strsplit(V2, ";"), "V2"), by = V1]`. This is already covered in the linked dupe by Jaap's answer – Mike H. Apr 26 '18 at 18:49

0 Answers0