R Basic

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

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

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

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

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

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

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

 

That’s it!

Share
Published by
Krunal Lathiya

Recent Posts

How to Append an Element to a List at Any Position in R

When you don't know the size of your data, you can use the "list" data…

2 months ago

How to Convert List to Numeric in R

To convert a list to a numeric value in R, you can combine the unlist()…

1 year ago

How to Use “lwd” in R

The lwd in R is used to specify the width of plot lines. It is…

1 year ago

How to Convert Double to Integer in R

To convert a double or float to an integer, you can use the "as.integer()" function.…

1 year ago

R dirname() Function

The dirname() function in R is used to extract the path to the directory from…

2 years ago

R month.abb

The month.abb is a built-in R constant, a three-letter abbreviation for the English month names. Example 1:…

2 years ago