as.numeric in R: How to Convert to Numeric Value

The numeric() method creates or coerces objects of type “numeric”.  The is.numeric() is a built-in function used for a more comprehensive test of an object being interpretable as numbers.

Factors are data structures that categorize or represent categorical data and save it on multiple levels. They can be saved as integers with an equal label to every unique integer.

R has two types when it comes to floating-point vectors.

  1. double: It is the name of a type.
  2. numeric: The mode’s name and the implicit class’s name.

Furthermore, R performs implicit data type coercion rules, which are needed when arithmetic operations are done on the vectors holding different types.

If an atomic vector includes a character string, each item in the vector is converted to the string. If the vector contains mixed elements of strings, numbers, or logicals, they are coerced to string types.

k <- c(21, "eleven", 19, 46)

kb <- c(T, "Ninteen", 18, 21, T, F)

kl <- c(11, T, 18, F, 6, "Meme")


[1] "character"
[1] "character"
[1] "character"

as.numeric in R

The as.numeric in R is a built-in method that returns a numeric value. The as.numeric() function takes an R object that needs to be coerced and returns the converted numeric value.

To convert factorial value to numeric value in R, use the as.numeric() function. The is.numeric() in R is a built-in function that checks whether the object can be interpretable as numbers.

The numeric() function is identical to the double() method. It creates a double-precision vector of the defined length, with each item equal to 0. The numeric() method takes a non-negative integer defining the desired length. Double values will be constrained to an integer. If you provide a parameter of length other than one is an error.

The default method is.numeric() returns TRUE if its parameter is of mode “numeric” (which can be of type “double” or “integer“) and not a factor. Otherwise, it returns FALSE.


numeric(length = 0)
as.numeric(x, …) 


length: It takes a non-negative integer to define the desired length.

x: It is an object to be coerced or tested.

…: It is the further arguments passed to or from other methods.


Let’s define the vector and convert it into a number using as.numeric() method.

rv <- c("-0.1", " 2.7 ", "3")


[1] -0.1 2.7 3.0

To check if the return value of as.numeric() function is a number or not, use is.numeric() method.

rv <- c("-0.1", " 2.7 ", "3")
x <- as.numeric(rv)


[1] TRUE

As you can see, the return value from as.numeric() function is a number because is.numeric() function returns TRUE.

Converting Factors to Numeric Values in R

To convert factors to the numeric value in R, use the as.numeric() function. If the input is a vector, use the factor() method to convert it into the factor and the as.numeric() method to convert the factor into numeric values.

When a factor is converted into a numeric vector, the numeric codes corresponding to the factor levels will be returned.

rv <- c("Mandalorian", "Ahshoka", "Obiwan")

# convert vector into factor
rf <- factor(rv)

# convert factor into numeric value
x <- as.numeric(rf)

# print the numeric value

# check if this is a numeric value


[1] 2 1 3
[1] TRUE

You can see that the output is factor levels, which is a numeric value; that is why it is.numeric() function returns TRUE.

Converting a Number Factor into a Numeric Value in R

To convert a number factor into a numeric value, use the as.character() and as.numeric() functions. For example, if the factor is a number, you first need to convert it to a character vector using the as.character() method and then use the as.numeric() method to convert it into a numeric.

If a factor is a character, you need not convert it into a character. And if you try converting an alphabet character to numeric, it will return NA.

rv <- c(19, 21, 11, 18, 46)

# creating a factor
rf <- factor(rv)

# converting a factor to numeric value
nv <- as.numeric(as.character(rf))

# check if it is numeric value


[1] 19 21 11 18 46
[1] TRUE


To convert any vector or factor into a numeric value in R, use the as.numeric() method. To check if the value is numeric, use the is.numeric() method. That’s it.

Leave a Comment