dummy_aggregate {SSBtools} | R Documentation |
aggregate_multiple_fun
using a dummy matrix
Description
Wrapper to aggregate_multiple_fun
that uses a dummy matrix instead of the by
parameter.
Functionality for non-dummy matrices as well.
Usage
dummy_aggregate(
data,
x,
vars,
fun = NULL,
dummy = TRUE,
when_non_dummy = warning,
keep_names = TRUE,
...
)
Arguments
data |
A data frame containing data to be aggregated |
x |
A (sparse) dummy matrix |
vars |
A named vector or list of variable names in
|
fun |
A named list of functions. These names will be used as suffixes in output variable names. Name can be omitted for one function.
A vector of function as strings is also possible. When unnamed, these function names will be used directly.
See the examples of |
dummy |
When |
when_non_dummy |
Function to be called when |
keep_names |
When |
... |
Further arguments passed to |
Details
Internally this function make use of the ind
parameter to aggregate_multiple_fun
Value
data frame
See Also
Examples
# Code that generates output similar to the
# last example in aggregate_multiple_fun
d2 <- SSBtoolsData("d2")
set.seed(12)
d2$y <- round(rnorm(nrow(d2)), 2)
d <- d2[sample.int(nrow(d2), size = 20), ]
x <- ModelMatrix(d, formula = ~main_income:k_group - 1)
# with specified output variable names
my_range <- function(x) c(min = min(x), max = max(x))
dummy_aggregate(
data = d,
x = x,
vars = list("freq", "y",
`freqmin,freqmax` = list(ra = "freq"),
yWmean = list(wmean = c("y", "freq"))),
fun = c(sum, ra = my_range, wmean = weighted.mean))
# Make a non-dummy matrix
x2 <- x
x2[17, 2:5] <- c(-1, 3, 0, 10)
x2[, 4] <- 0
# Now warning
# Result is not same as t(x2) %*% d[["freq"]]
dummy_aggregate(data = d, x = x2, vars = "freq", fun = sum)
# Now same as t(x2) %*% d[["freq"]]
dummy_aggregate(data = d, x = x2,
vars = "freq", dummy = FALSE,
fun = function(x, y) sum(x * y))
# Same as t(x2) %*% d[["freq"]] + t(x2^2) %*% d[["y"]]
dummy_aggregate(data = d, x = x2,
vars = list(c("freq", "y")), dummy = FALSE,
fun = function(x, y1, y2) {sum(x * y1) + sum(x^2 * y2)})