Understanding the as.numeric() Function in R

Data occasionally comes in various formats, including numeric values stored as text (characters) or categories (factors).

When preparing the data for further analysis, you must ensure it is in the correct format.

To perform numerical operations, you need these in numeric format, and that’s where as.numeric() function comes into play!

What is the as.numeric function?

The as.numeric() function converts valid non-numeric data into numeric data. What do I mean by proper numeric data? Well, numeric values are stored as text or categorical variables (factors).

Proper use of as.numeric() function in R

If you try to use the as.numeric() function on non-numeric characters, it returns NA for those values.

Use the as.numeric() function on non-numeric characters

Syntax

as.numeric(character)

Parameters

Name Description
character A character vector

Return value

It returns a numeric vector object.

Example 1: Basic usage

vec <- c("1", "2", "3")

as.numeric(vec)

Output

[1] 1  2  3

You can use the is.numeric() method to check if the value is numeric.

vec <- c("1", "2", "3")

numeric_vec <- as.numeric(vec)

is.numeric(numeric_vec)

Output

[1] TRUE

Example 2: Converting a factor to a numeric vector

Convert factor to numeric

In the above figure, we defined a factor called “fac” and converted it to a numeric vector.

In factor type, each level, a unique string value, is replaced by its corresponding integer code.

Factor levels are encoded as integers starting with 1.

So, if “Mandalorian” is the first level, its corresponding integer code is 1, “Ahshoka” the second, and “Obiwan” the third, they will be replaced by 1, 2, and 3, respectively.

# convert vector into factor
fac <- factor(vec)

# convert factor into numeric value
num <- as.numeric(fac)

# print the numeric value
num

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

Output

[1] 2 1 3

[1] TRUE

Example 3: Converting logical vector to numeric vector

Converting logical vector to numeric vector

The above figure shows that we have a logical vector converted to a numeric vector, and values FALSE become 0 and TRUE become 1.

vec_logical <- c(FALSE, FALSE, TRUE, TRUE)

vec_num <- as.numeric(vec_logical)

vec_num

Output

[1] 0 0 1 1

Example 4: Converting a list to a numeric vector

Pictorial representation of Converting a list to a numeric vector

In this figure, we directly get the numeric vector in the output without needing an unlist() function.

num_list <- list(11, 21, 19, 46)

num_vec <- as.numeric(num_list)

num_vec

Output

[1] 11 21 19 46

Example 5: NAs introduced by coercion

Pictorial Representation NAs introduced by coercion

The above figure shows that if you pass a list of mixed values, such as numeric and character values, then character values will be converted to NA, and numeric values will remain the same.

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

That’s it!

Leave a Comment