I have a large (millions of rows) melting data.tablewith the usual meltsterile unfolding in variableand columns value. I need to lay out the table in a wide form (turning the variables up). The problem is that the data table also has a list column datathat I need to save. This makes use impossible reshape2because it dcastcannot deal with non-atomic columns. Therefore, I need to do self-fulfillment.
The answer from the previous question about working with tables of molten data is not applied here because of the list column.
I am not satisfied with the solution I came up with. I am looking for suggestions for a simpler / faster implementation.
x <- LETTERS[1:3]
dt <- data.table(
x=rep(x, each=2),
y='d',
data=list(list(), list(), list(), list(), list(), list()),
variable=rep(c('var.1', 'var.2'), 3),
value=seq(1,6)
)
list_template <- Reduce(
function(l, col) { l[[col]] <- col; l },
unique(dt$variable),
list())
q <- substitute({
l <- lapply(
list_template,
function(col) .SD[variable==as.character(col)]$value)
l$data = .SD[1,]$data
l
}, list(list_template=list_template))
# Roll up
dt[, eval(q), by=list(x, y)]
x y var.1 var.2 data
1: A d 1 2 <list>
2: B d 3 4 <list>
3: C d 5 6 <list>