Description Usage Arguments Details Value See Also Examples
View source: R/data.frame-methods.R
dtabs
is a simplified version of function
xtabs
, designed specifically for
constructing demographic arrays.
1 |
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
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.