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!

as.numeric() function

R 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 It is a character vector

Return value

It returns a numeric vector object.

Example 1: Converting a character vector to numeric

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: Handling non-numeric characters

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

Example 6: Plotting

Let’s plot the chart of temperature of a town based on the date(day-by-day). To plot a chart, we need to convert character dates to Date objects and then convert the dates into numeric values using as.numeric() function.

dates <- c("2024-09-21", "2024-09-22", "2024-09-23", "2024-09-24", "2024-09-25")
temperatures <- c(5, 8, 12, 15, 20)

# Converting character dates to Date objects

date_objects <- as.Date(dates)

# Converting dates to numeric (days since 1970-01-01)

date_numeric <- as.numeric(date_objects)

# Plotting

plot(date_numeric, temperatures, type = "b", xaxt = "n",
 main = "Temperature Over Time")

axis(1, at = date_numeric, labels = format(date_objects, "%b %d"), las = 2)

Output

 

Plotting of as.numeric()

That’s it!

Leave a Comment