I have a data.table in R where one column C contains vectors, e.g. c('a', 'b', 'c'). I want to expand the vector into rows in the data.table, so that for the value c('a', 'b', 'c') in column C, it will generate three rows with the other columns copied and the value in column C being a, b, and c respectively.
library(data.table)
DT <- data.table(
A = c(1, 2, 3),
B = c("abc", "bbc", "ccc"),
C = list(c("a", "b", "c"), c("d", "e"), c("f"))
)
DT
A B C # in real data, there are many cols
1: 1 abc a,b,c
2: 2 bbc d,e
3: 3 ccc f
my expacted:
A B C
1: 1 abc a
2: 1 abc b
3: 1 abc c
4: 2 bbc d
5: 2 bbc e
6: 3 ccc f
I find a code with tidyverse
from expanding R data.table by splitting on one column , but I dont want to include extra library if possible.
How can I achieve this using data.table syntax in R?