Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

numerics

Numeric vectors


Description

Coerces objects to class "numeric", and checks if an object is numeric.

Usage

asNumeric(x)
possibleNumeric(x)
wholeNumeric(x)

Arguments

x

A vector of values

Details

Unlike the function as.numeric() from the base package, the function asNumeric() coerces to numeric without a warning if any values are not numeric. All such values are considered NA missing.

The function possibleNumeric() tests if the values in a vector are possibly numeric, irrespective of their storing as character or numbers.

Function wholeNumeric() tests if numbers in a vector are whole (round) numbers. Whole numbers are different from “integer” numbers (which have special memory representation), and consequently the function is.integer() tests something different, how numbers are stored in memory (see the description of function double() for more details).

Author(s)

Adrian Dusa

See Also

Examples

x <- c("-.1", " 2.7 ", "B")
asNumeric(x) # (-0.1, 2.7, NA)  and no warning

possibleNumeric(x) # FALSE

possibleNumeric(c("1", 2, 3)) # TRUE

is.integer(1) # FALSE

# Signaling an integer in R 
is.integer(1L) # TRUE

wholeNumeric(1) # TRUE

admisc

Adrian Dusa's Miscellaneous

v0.12
GPL (>= 3)
Authors
Adrian Dusa [aut, cre, cph] (<https://orcid.org/0000-0002-3525-9253>)
Initial release
2021-03-16

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.