dtabs {dembase} | R Documentation

## Simplified version of 'xtabs' for constructing demographic arrays

### Description

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

### Usage

    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 `NA`s 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

    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)

[Package dembase version 0.0.0.119 Index]