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

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

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

as.numeric in R

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

The numeric() function is identical to 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 the parameter of length other than one is an error.

The is.numeric() is an inbuilt generic R function that checks if the object can be interpretable as numbers or not.

The default method for 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.

Syntax

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

Parameters

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

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

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

Example

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

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

Output

[1] -0.1 2.7 3.0

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

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

Output

[1] TRUE

As you can see that 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 numeric value in R, use the as.numeric() function. If the input is a vector, then use the factor() method to convert it into the factor and then use 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
x

# check if this is a numeric value
is.numeric(x)

Output

[1] 2 1 3
[1] TRUE

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

Converting a Number Factor into Numeric Value

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

If a factor is a character, then you need not convert it to 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))
nv

# check if it is numeric value
is.numeric(nv)

Output

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

Conclusion

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

Leave a Comment