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!
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.
as.numeric(character)
Name | Description |
character | It is a character vector |
It returns a numeric vector object.
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
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
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
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
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
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!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
When you don't know the size of your data, you can use the "list" data…
To convert a list to a numeric value in R, you can combine the unlist()…
To convert a double or float to an integer, you can use the "as.integer()" function.…
The dirname() function in R is used to extract the path to the directory from…
The month.abb is a built-in R constant, a three-letter abbreviation for the English month names. Example 1:…