Learn R Programming

rmngb (version 0.6-1)

asInteger: Floating Point Numbers to Integers

Description

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

Usage

asInteger(x)

Arguments

x
object to be coerced.

Value

An integer vector.

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.

See Also

as.integer, round.

Examples

Run this code
(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"

Run the code above in your browser using DataLab