coerce-data {dembase}R Documentation

Coerce to numeric or integer.

Description

Function toInteger uses round followed by as.integer to coerce the data part of a DemographicArray object to type "integer". Function toDouble uses as.double to coerce to type "double". If the coercion to type "integer" requires rounding, then an error is raised, unless force is TRUE.

Usage

toDouble(object)

toInteger(object, force = FALSE)

## S4 method for signature 'DemographicArray'
toDouble(object)

## S4 method for signature 'DemographicArray'
toInteger(object, force = FALSE)

## S4 method for signature 'numeric'
toDouble(object)

## S4 method for signature 'integer'
toInteger(object, force = FALSE)

## S4 method for signature 'numeric'
toInteger(object, force = FALSE)

Arguments

object

An object of class DemographicArray or a numeric vector.

force

TRUE or FALSE (the default).

See Also

as.integer, which, unlike toInteger, removes all attributes.

Examples

## A demographic array.
x <- Counts(array(c(1, 2, 3, 4),
                  dim = c(2, 2),
                  dimnames = list(sex = c("Female", "Male"),
                      region = c("a", "b"))))

## x consists entirely of whole numbers,
## but has type "double", not "integer":
is.integer(x)
x <- toInteger(x)
is.integer(x)
x[1] <- 1.1

## rounding is required, so 'toInteger' won't work
## Not run: toInteger(x)
## ...unless 'force' is TRUE
toInteger(x, force = TRUE)

## A numeric vector
x <- c(1, 2, 3)
is.integer(x)
x <- toInteger(x)
x
is.integer(x)
x <- toDouble(x)
x
is.integer(x)

[Package dembase version 0.0.0.119 Index]