The function sweep() is underrated. So, to expand upon its benefits, how should a multivariate version of sweep() resemble?
# Rough draft
meep <- function(f, s, x, ..., keep.rownames = FALSE) {
# function, summary statistic, dataset, mapply inputs, rownames condition
output <- mapply(function(y) f(y, s(y)), x, ...)
cond_len <- length(NROW(x)) == length(NROW(output))
if (length(dim(x)) > 1) {
if (keep.rownames == TRUE & cond_len == FALSE) {
stop('length(NROW(x)) != length(NROW(output))')
} else if (keep.rownames == TRUE & cond_len == TRUE) {
rownames(output) <- rownames(x)
}
}
output
}
# Example
meep(`/`, mean, mtcars, keep.rownames = TRUE)