Description Usage Arguments Details Examples
Wrappers for working with numeric vectors
1 2 3 4 5 | to_numeric(x, int = TRUE)
maybe_numeric(x, names = FALSE)
maybe_integer(x, names = FALSE)
|
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. |
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.
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.