as.data.frame {dembase} | R Documentation

## Convert a DemographicArray to a data.frame

### Description

Convert a `DemographicArray` object to a data frame.

### Usage

    ## S4 method for signature 'Counts'
    as.data.frame(
      x,
      row.names = NULL,
      optional = FALSE,
      stringsAsFactors = FALSE,
      responseName = "count",
      direction = c("long", "wide"),
      midpoints = FALSE,
      ...
    )

    ## S4 method for signature 'Values'
    as.data.frame(
      x,
      row.names = NULL,
      optional = FALSE,
      stringsAsFactors = FALSE,
      responseName = "value",
      direction = c("long", "wide"),
      midpoints = FALSE,
      ...
    )

### Arguments

`x` | Object of class `DemographicArray`.
`row.names` | 'NULL' or a character vector giving the row names for the data frame. See `as.data.frame`.
`optional` | Logical. See `as.data.frame`.
`stringsAsFactors` | Logical. If `TRUE` factors are used to represent qualitative variables. Defaults to `FALSE`.
`responseName` | Name of column with numeric values when `direction` is `"long"`. Defaults to `"count"` if `object` is `Counts` and `"value"` if `object` is `Values`.
`direction` | Character. Whether data are converted to `"long"` format (the default) or `"wide"` format.
`midpoints` | `FALSE` (the default), `TRUE`, or names of dimensions with `dimscale` `"Intervals"`.
`...` | Additional arguments to be passed to or from methods.

### Details

If `direction` is `"long"`, then `object` is put into "long" format, with a column to represent each dimension, plus a final column holding the data. The final column is called `"count"` if `object` has class `Counts` and `"value"` if `object` has class `Counts`.

If `direction` is `"wide"`, then `object` is flattened into a matrix before conversion to a data frame, which is what the method for ordinary arrays does.

If `midpoints` is `FALSE`, interval labels are converted into factors. If `midpoints` is `TRUE`, interval labels are replaced by numbers giving their midpoints. If names of individual dimensions are supplied, then only the named dimensions are converted.

### Value

A data frame.

### Warning

By default, the `as.data.frame` methods for demographic arrays convert the arrays to long format, since this is almost always what is wanted. This is different from the `as.data.frame` method for ordinary arrays in the `base` package, which always converts to wide format.

### See Also

`as.data.frame`, `as.data.frame.table`, `data.frame`, `midpoints`

### Examples

    library(demdata)
    popn <- Counts(VAPopn)
    as.data.frame(popn)
    as.data.frame(popn, direction = "wide")

    x <- Values(array(1:4,
                      dim = c(2, 2),
                      dimnames = list(age = c("0-39", "40+"),
                          period = c("2001-2010", "2011-2020"))))
    as.data.frame(x)
    as.data.frame(x, midpoints = TRUE)
    as.data.frame(x, midpoints = "period")

[Package dembase version 0.0.0.119 Index]