Search code examples
rcastingdata.tabledcast

data.table - split data.table into elements of a lists using ~ var|factor - cast / dcast


I have a large data.table which I am using for subsequent analysis on different levels of the data. In order to avoid repetitive code, I would like to break my data.table into smaller chunks and save them as (elements of) lists. Couple of days ago I have seen a post where something like this was used:

setDT(mtcars)
names(mtcars)
combi <- dcast(mtcars[,.(carb, gear, mpg, cyl, vs)], cyl + gear ~ vs|cyl)

This resulted in a separate (wide) data.table containing cyl, gear, vs - separately for year cyl. The emphasis is on saving separate data.table for every cyl, not necessarily on wide table (I believe the post used cast or dcast to achieve the desired results)

However, I cannot find the post anywhere and also cannot find any documentation about similar manipulations. Any ideas?


Solution

  • The following shows how to get datatables for each level of cyl into a list - if you want to write them to a .txt file etc. then just alter that section to be fwrite(...). Note it is working through in the order that cyl has in the data (i = 1 is when cyl = 6, i = 2 is when cyl = 4...):

    dtCars <- data.table(mtcars, keep.rownames = TRUE)
    carsList <- list()
    for(i in 1:length(dtCars[,unique(cyl)])){
      carsList[[i]] <- dtCars[cyl == unique(cyl)[i]]
    }