Created
November 23, 2017 05:35
-
-
Save bfatemi/3b011ef5c86f1153ea529bfd73e3aee8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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