coerce-data: Coerce to numeric or integer.

Description Usage Arguments See Also Examples

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
## 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)

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