Skip to content

Instantly share code, notes, and snippets.

@bfatemi
Created November 23, 2017 05:35
Show Gist options
  • Save bfatemi/3b011ef5c86f1153ea529bfd73e3aee8 to your computer and use it in GitHub Desktop.
Save bfatemi/3b011ef5c86f1153ea529bfd73e3aee8 to your computer and use it in GitHub Desktop.
DT <- as.data.table(iris)
DT[, MONTHS := sample(month.abb, size = .N, replace = TRUE)]
DT[, LETTERS := sample(LETTERS[rep(1:5, 3)], size = .N, replace = TRUE)]
DT
grp1 <- c("Species", "LETTERS")
grp2 <- c("MONTHS")
resDT <- DT[, .(grpDT = {
res <- list(as.data.table(.SD)[, .N, grp2])
res
}), grp1]
## SUM OF COUNTS IN ALL SEPTEMBERS ACROSS LETTERS FOR THE SPECIES SETOSA
sum(unlist(lapply(resDT[Species == "setosa", grpDT], function(dt) dt[MONTHS == "Sep", N])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment