convert character type vector to numeric type in R

Convert Character to Numeric in R (With Examples)

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?

convert character type vector to numeric type 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”

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

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


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top