DummyApply | R Documentation |
For each column, i
, of the matrix x
of zeros and ones, the output value is equivalent to FUN(y[x[, i] != 0])
.
DummyApply(x, y, FUN = sum, simplify = TRUE)
x |
A (sparse) dummy matrix |
y |
Vector of input values |
FUN |
A function |
simplify |
Parameter to |
With a dummy x
and FUN = sum
, output is equivalent to z = t(x) %*% y
.
Vector of output values or a matrix when multiple outputs from FUN
(see examples).
List output is also possible (ensured when simplify = FALSE
).
z <- SSBtoolsData("sprt_emp_withEU")
z$age[z$age == "Y15-29"] <- "young"
z$age[z$age == "Y30-64"] <- "old"
a <- ModelMatrix(z, formula = ~age + geo, crossTable = TRUE)
cbind(as.data.frame(a$crossTable),
sum1 = (t(a$modelMatrix) %*% z$ths_per)[,1],
sum2 = DummyApply(a$modelMatrix, z$ths_per, sum),
max = DummyApply(a$modelMatrix, z$ths_per, max))
DummyApply(a$modelMatrix, z$ths_per, range)
DummyApply(a$modelMatrix, z$ths_per, range, simplify = FALSE)
a$modelMatrix[, c(3, 5)] <- 0 # Introduce two empty columns.
DummyApply(a$modelMatrix, z$ths_per, function(x){
c(min = min(x),
max = max(x),
mean = mean(x),
median = median(x),
n = length(x))})
DummyApply(a$modelMatrix, z$ths_per, function(x) x, simplify = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.