In this tutorial, we will look at how to convert a character type field (for example, a vector or a dataframe column) to a numeric type in R.
How to convert character type data to numeric in R?
You can use the as.numeric()
function in R to convert character type to numeric type in R. Pass the field (for example, a vector) as an argument to the function. The following is the syntax –
as.numeric(x)
If you pass a vector to the above function, it returns a vector with each value in numeric type.
Examples
Let’s now look at some examples of using the as.numeric()
function in R.
Convert character type vector to numeric type vector
Let’s create a vector with numbers as character type values and then apply the as.numeric()
function.
# create a vector vec <- c('1', '2', '3', '4') # convert to numeric vec <- as.numeric(vec) # display the vector print(vec) # display vector's type print(class(vec))
Output:
[1] 1 2 3 4 [1] "numeric"
The resulting vector has numeric values.
Note that this method will not work if the character type values cannot be converted to numeric type. For example, “a”
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# create a vector vec <- c('a', 'b', 'c', 'd') # convert to numeric vec <- as.numeric(vec) # display the vector print(vec) # display vector's type print(class(vec))
Output:
Warning message in eval(expr, envir, enclos): “NAs introduced by coercion” [1] NA NA NA NA [1] "numeric"
We get a vector of NA
values and a warning. This is because the original values from the vector cannot be converted to numeric.
Convert character type dataframe column to numeric type
You can also use the as.numeric()
function to change the data type of a dataframe column in R to numeric.
Let’s look at an example.
First, we will create a dataframe with the height and weight information of some students in a university.
# create a data frame df <- data.frame( "Name"= c("Tim", "Hasan", "Vlad", "Maria"), "Height"= c("168", "174", "162", "158"), "Weight"= c(73, 81, 65, 55) ) # display the dataframe print(df)
Output:
Name Height Weight 1 Tim 168 73 2 Hasan 174 81 3 Vlad 162 65 4 Maria 158 55
You can see that the “Height” column in the above dataframe is of character type. Let’s change its type to numeric type using the as.numeric()
function.
# convert "Height" column to numeric type df$Height <- as.numeric(df$Height) # display "Height" column's type print(class(df$Height))
Output:
[1] "numeric"
You can see that the “Height” column is now of numeric type.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.