 # as.numeric In R: Convert To Numeric Value This tutorial will discuss converting an object to a numeric by using the as.numeric() function in the R programming language. If you are interested in this topic, read the end of this article now.

## What is the as.numeric in R?

The as.numeric() function returns a numeric value in R. The as.numeric() function takes a R object and converts it to a numeric value.

We will use the as.numeric() function to convert a factorial value to a numeric number and use the is.numeric() function to examine if an object may be interpreted as numbers.

The numeric() function is the same as the double() method that generates a double-precision vector of the specified length, with each item having the value 0. The numeric() method accepts a non-negative integer describing the required length as an argument. Double values will be limited to integers, which is an error if you offer a parameter with a length different than one.

The default is.numeric() function returns TRUE if its parameter is of type “double” or “integer” and mode “numeric” else, it returns FALSE.

Now, you can follow the syntax of this function below to see how this function works.

The syntax of as.numeric function in R:

as.numeric(x)
is.numeric(x)

Parameter:

• x: An object which you want to convert to numeric.

## How to use the as.numeric in R?

### Convert character vector to numeric values

First, we will create a character vector for this example as below:

# Create a character vector
vec <- c("10", "20", "7.5", "-9")

# View a vector
cat("The vector is:\n")
vec

Output

The vector is:
 "10"  "20"  "7.5" "-9" 

Now, we will use the as.numeric() function in R to convert all elements in the vector to numeric values, and then you can check the convert above success or not success by using the is.numeric() function.

# Create a character vector
vec <- c("10", "20", "7.5", "-9")

# Convert to numeric value
res <- as.numeric(vec)

# View a vector
cat("The vector after conversion is:\n")
res

# Check a numeric value
is.numeric(res)

Output

The vector after conversion is:
 10.0 20.0  7.5 -9.0
 TRUE

### Convert factors to numeric values

We will convert factors to numeric values using the as.numeric() function in this example. If the input is a character vector, use the factor() function to convert it to a factor and the as.numeric() function to convert the factor to numeric values. Follow the code example.

# Create a character vector
vec <- c("Bray", "Seachains", "Amee", "Wowy")

# Convert a vector into a factor
vf <- factor(vec)

# Convert a factor into a numeric value
res <- as.numeric(vf)

# View a the numeric value
cat("The vector after conversion is:\n")
res

# Check a numeric value
is.numeric(res)

Output

The vector after conversion is:
 2 3 1 4
 TRUE

Another example similarly you can convert a number factor to a numeric value. Check out the code example below.

# Create a  vector
vec <- c(8, 9, 12, -5, 20, -19)

# Convert a vector into a factor
vf <- factor(vec)

# Convert a factor into a numeric value
res <- as.numeric(as.character(vf))

# View a the numeric value
cat("The vector after conversion is:\n")
res

# Check a numeric value
is.numeric(res)

Output

The vector after conversion is:
   8   9  12  -5  20 -19
 TRUE

## Summary

So, this tutorial will demonstrate how to use the as.numeric() function in the R programming language to convert a vector to a numeric value. If you have any questions, please leave a comment below.

Good luck!

Maybe you are interested:

Posted in R