How to Use as.numeric() Function in R (6 Examples)

The as.numeric() function in R is “used to convert a character vector into a numeric vector”.

Syntax

as.numeric(character)

Parameters

character: It is a character vector to be converted into a numeric vector.

Return value

It returns a numeric vector object.

Example 1: Converting a character vector to a numeric vector

Create a character vector using the “c()” function and convert it into a numeric vector using the “as.numeric()” method.

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

Output

[1] -0.1 2.7 3.0

Example 2: Use of is.numeric() function

To check if the return value of the “as.numeric()” function is numeric, you can use the “is.numeric()” method.

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

Output

[1] TRUE

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

Example 3: Converting factors to numeric vectors in R

To convert factors to the numeric value in R, you can also use the “as.numeric()” function.

The as.numeric() function will return the factor levels as output and not the factor itself.

If the input is a vector, you can use the “factor()” method to convert it into the factor and the as.numeric() method to convert the factor into numeric values.

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, a numeric value; that is why the “is.numeric()” function returns TRUE.

Example 4: Converting logical vector to numeric vector in R

Logical vectors whose values are like “TRUE” or “FALSE” can be converted to numeric values using the “as.numeric()” function.

vec_logical <- c(FALSE, FALSE, TRUE, TRUE)
vec_num <- as.numeric(vec_logical)
vec_logical

Output

[1] FALSE FALSE TRUE TRUE

Example 5: Converting a list to a numeric vector in R

You can use the as.numeric() function to convert a list to a numeric vector in R.

Provide a list to the as.numeric() function, which returns the numeric vector.

num_list <- list(11, 21, 19, 46)
number <- as.numeric(num_list)
number

Output

[1] 11 21 19 46

Example 6: NAs introduced by coercion

If the as.numeric() function cannot convert any element to a number, it returns NA for that element.

num_list <- list(11, "KL", 19, "KB")
number <- as.numeric(num_list)
number

Output

Warning messages:
1: NAs introduced by coercion
2: NAs introduced by coercion
[1] 11 NA 19 NA

The character strings “KL” and “KB” cannot be converted to numeric values in the above code. That’s why NAs are returned.

If the input value is a vector of characters, as.numeric() function tries to convert the characters to numbers.

If the input value is a factor, the as.numeric() function will return the factor levels as a numeric vector.

Leave a Comment