d <- SSBtoolsData("d2")[1:20, ]
d[[2]] <- as.numeric(d[[2]])
d$y <- as.numeric(1:20)
d$y[2] <- NA
d$county[8:9] <- NA
d$main_income[11:12] <- NA
d$k_group[19:20] <- NA
by <- c("main_income", "county", "k_group")
a1 <- aggregate_by_pkg(d, by = by, var = c("y", "freq"))
a2 <- aggregate_by_pkg(d, by = by, var = c("y", "freq"),
include_na = TRUE)
a3 <- aggregate_by_pkg(d, by = by, var = c("y", "freq"),
include_na = TRUE, fun = function(x) list(x))
if (requireNamespace("data.table", quietly = TRUE)) {
b1 <- aggregate_by_pkg(d, by = by, var = c("y", "freq"), pkg = "data.table")
b2 <- aggregate_by_pkg(d, by = by, var = c("y", "freq"), pkg = "data.table",
include_na = TRUE)
b3 <- aggregate_by_pkg(d, by = by, var = c("y", "freq"), pkg = "data.table",
include_na = TRUE, fun = function(x) list(x))
print(identical(a1, b1)) # TRUE when base_order succeeds
print(identical(a2, b2))
print(identical(a3, b3))
} else {
print("The 'data.table' package is not installed.")
}
Run the code above in your browser using DataLab