to_numeric: Numeric Vectors - expanded

Description Usage Arguments Details Examples

View source: R/numerics.R

Description

Wrappers for working with numeric vectors

Usage

1
2
3
4
5

Arguments

x

A vector

int

Logical. Integer control, if TRUE will determine if vector may be integers.

names

Logical. If true, supplies original value as name.

Details

Unlike, 'as.numeric()', 'to_numeric()' will return outpus as integers or as doubles.

Factors are also specially handled as a numeric value is derived from the factor value rather than the factor level.

'maybe_numeric' can be used to check is a value, possibly stored as text, can be safely converted. When 'int = TRUE', 'maybe_integer()' is employed to evaluation the vector. If all values could be integers, the vector is returned as an integer.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
## with characters
x <- c(as.character(1:5), NA, letters[1:2], "1e4")
suppressWarnings(as.numeric(x))  ## will throw an error
to_numeric(x)                    ## converts NA to NaN
maybe_numeric(x, names = TRUE)

## With factors
f <- factor(c(seq(0, 1, .2), "No", "Na_character_"))
suppressWarnings(as.numeric(f))  ## uses factor level instead of text
to_numeric(f)                    ## uses text values instead
maybe_numeric(f, names = TRUE)

jmbarbone/dirtyr documentation built on Sept. 23, 2020, 4:05 a.m.