Reputation: 1327
How can I group a data.table
e.g. with filenames by metadata and store each group in a list
-item?
Input:
DT <- data.table(files = 1:10, meta = c(rep(1, 6), rep(2,4)))
I thought of something like:
MyList <- DT[, files, by = meta] #something like that
Desired Output:
[[1]]
[1] 1 2 3 4 5 6
[[2]]
[1] 7 8 9 10
The above example is simplified in the case that meta are actually more columns (to group by) and some rows are selected before.
Is there a simple way to go (preferable a data.table
-solution), or do I have to use some other way like
lapply(unique(DT$meta), function(x) DT[meta == x]$files)
?
Upvotes: 0
Views: 34
Reputation: 887391
We can wrap it in a list
DT1 <- DT[, .(files = list(files)), meta]
DT1$files
#[[1]]
#[1] 1 2 3 4 5 6
#[[2]]
#[1] 7 8 9 10
Upvotes: 1