← All languages
R function of the day

as.numeric

Convert an object to numeric type.

Description

The as.numeric() function coerces an R object to double-precision numeric type. It converts character strings that represent numbers, logical values (TRUE becomes 1, FALSE becomes 0), and factors (converted by their internal integer codes).

If the conversion is not possible, NA is returned with a warning. as.numeric is commonly used when reading data that was imported as character type. Related functions include as.integer(), as.character(), and as.logical().

Arguments

NameDescriptionOptional
x An object to be coerced to numeric. No

Example

as.numeric("3.14")           # 3.14
as.numeric(TRUE)              # 1
as.numeric(c("1", "2", "3"))  # 1 2 3
as.numeric("abc")             # NA (with warning)

Reference