dtabs: Simplified version of 'xtabs' for constructing demographic...

Description Usage Arguments Details Value See Also Examples

View source: R/data.frame-methods.R

Description

dtabs is a simplified version of function xtabs, designed specifically for constructing demographic arrays.

Usage

1
dtabs(data, formula, na.rm = FALSE, fill = 0L)

Arguments

data

A data.frame or matrix.

formula

A formula: see xtabs for details.

na.rm

Logical. Whether to remove NAs before tabulating. Defaults to FALSE.

fill

The value to use for combinations of variables that do not occur in data.

Details

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.

Value

An array.

See Also

dtabs is based on xtabs. To turn a plain array created by dtabs into a demographic array, use function Counts or Values.

Examples

 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)

StatisticsNZ/dembase documentation built on Dec. 25, 2021, 4:49 p.m.