# 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 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.

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

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

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

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

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

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

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

#### Output

`````` "character"
 "character"
 "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 if the object can be interpretable as numbers or not.

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 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

`` -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

`` 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 the 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

`````` 2 1 3
 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 in R

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

`````` 19 21 11 18 46
 TRUE``````

## Conclusion

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.

Categories R