| dtabs {dembase} | R Documentation |
dtabs is a simplified version of function
xtabs, designed specifically for
constructing demographic arrays.
dtabs(data, formula, na.rm = FALSE, fill = 0L)
data |
A data.frame or matrix. |
formula |
A formula: see |
na.rm |
Logical. Whether to remove |
fill |
The value to use for combinations of
variables that do not occur in |
The data argument comes first, so that dtabs
works nicely with pipes.
The fill argument makes it easy to control what
value used for combinations of classifying variables
that do not occur in the original dataset data.
See below for examples.
The return value is a plain array, not an
xtabs object.
Unlike xtabs, dtabs has no subset argument.
Rather than being combined with the tabulation, subsetting
should be separated out into its own operation, via a
function such as subset or filter.
An array.
dtabs is based on xtabs. To turn
a plain array created by dtabs into a demographic array,
use function Counts or Values.
d <- data.frame(age = c("young", "old", "young", "old"),
sex = c("Female", "Female", "Male", "Male"),
count = 1:4)
dtabs(d, count ~ age + sex)
dtabs(d, count ~ age)
dtabs(d, ~ age + sex)
dtabs(d, ~ age)
d_incomplete <- data.frame(age = c("young", "old", "young"),
sex = c("Female", "Female", "Male"),
count = 1:3)
## default value of fill is 0
dtabs(d_incomplete, count ~ age + sex)
dtabs(d_incomplete, count ~ age + sex, fill = NA)