0

I am having a data.table with a tab separated string that I want to separate into new columns. However, if I slice by index, I just get the first element of the first row for every field. How do I do this?

library(data.table)
a <- c("feature1\titem1\titem2")
dt1 <- data.table(a)
a <- c("feature2\titem3\titem4")
dt2 <- data.table(a)
dt <- rbindlist(list(dt1, dt2))
dt[, split := mapply(str_split, a, "\t", n = 2)]
# how to get a feature column from that?
gernophil
  • 177
  • 2
  • 6

1 Answers1

0

One possible solution with transpose:

dt[, transpose(stringr::str_split(a,"\t"))]
         V1     V2     V3
     <char> <char> <char>
1: feature1  item1  item2
2: feature2  item3  item4
Waldi
  • 39,242
  • 6
  • 30
  • 78