as_numeric: Test object for, or coerce to, numeric

Description Usage Arguments Examples

View source: R/as_numeric.R

Description

as_numeric is essentially a wrapper to as.numeric except that objects of class factor are first coerced to character and then to numeric. is_numeric test if x is "somehow numeric" (see examples).

Usage

1
2
3

Arguments

x

object to be coerced or tested (and return a logical vector of the same length) or should it test the whole vector as one object and return a logical vector of length one. (TRUE by default).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
df <- data.frame(v = c("46513", "45"))
class(df$v) # factor

# Note that
as.numeric(df$v) # 2 1
# but
as_numeric(df$v) # 46513    45

is_numeric(1) # TRUE
is_numeric("1") # TRUE
is_numeric(as.factor(1)) # TRUE
is_numeric(as.factor("khb")) # FALSE

incadata documentation built on April 14, 2020, 6:08 p.m.