asInteger: Floating Point Numbers to Integers

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/utils.R

Description

When numbers should be integers but aren't because of floating point arithmetic issues.

Usage

1

Arguments

x

object to be coerced.

Details

Standard coercion to integer using as.integer truncates floats. That behaviour can produce unexpected results when the number is used for subsetting or in a comparaison.

Value

An integer vector.

Author(s)

Antoine Filipovic Pierucci

See Also

as.integer, round.

Examples

1
2
3
4
5
6
7
(z <- .3 / .1) == 3 # FALSE
as.integer(z) == 3 #FALSE
asInteger(z) == 3 # TRUE

x <- letters[1:5]
x[z] # not "c"
x[asInteger(z)] # "c"

Example output

[1] FALSE
[1] FALSE
[1] TRUE
[1] "b"
[1] "c"

rmngb documentation built on May 29, 2017, 9:22 p.m.

Related to asInteger in rmngb...