How can I convert a character to numeric in R, and what are some examples of doing so?

In R, there are several methods for converting a character to a numeric value. The most common approach is to use the as.numeric() function, which takes a character input and returns the numerical equivalent. Other options include using the as.integer() or as.double() functions, depending on the desired output type.

Examples of converting a character to numeric in R include converting a string of numbers, such as “123”, to its numeric value of 123, or converting a string of letters, such as “ABC”, to its ASCII numerical representation. Another example is converting a character vector containing multiple elements, such as “1”, “2”, and “3”, to a numeric vector of 1, 2, and 3. It is important to note that if the character input cannot be converted to a numeric value, R will return an error.

Convert Character to Numeric in R (With Examples)


We can use the following syntax to convert a character vector to a numeric vector in R:

numeric_vector <- as.numeric(character_vector)

This tutorial provides several examples of how to use this function in practice.

Example 1: Convert a Vector from Character to Numeric

The following code shows how to convert a character vector to a numeric vector:

#create character vector
chars <- c('12', '14', '19', '22', '26')

#convert character vector to numeric vector
numbers <- as.numeric(chars)

#view numeric vector
numbers

[1] 12 14 19 22 26

#confirm class of numeric vector
class(numbers)

[1] "numeric"

Example 2: Convert a Column from Character to Numeric

The following code shows how to convert a specific column in a data frame from character to numeric:

#create data frame
df <- data.frame(a = c('12', '14', '19', '22', '26'),
                 b = c(28, 34, 35, 36, 40))

#convert column 'a' from character to numeric
df$a <- as.numeric(df$a)

#view new data frame
df

   a  b
1 12 28
2 14 34
3 19 35
4 22 36
5 26 40

#confirm class of numeric vector
class(df$a)

[1] "numeric"

Example 3: Convert Several Columns from Character to Numeric

The following code shows how to convert all character columns in a data frame from character to numeric:

#create data frame
df <- data.frame(a = c('12', '14', '19', '22', '26'),
                 b = c('28', '34', '35', '36', '40'),
                 c = as.factor(c(1, 2, 3, 4, 5)),
                 d = c(45, 56, 54, 57, 59))

#display classes of each column
sapply(df, class)

          a           b           c           d 
"character" "character"    "factor"   "numeric" 

#identify all character columns
chars <- sapply(df, is.character)

#convert all character columns to numeric
df[ , chars] <- as.data.frame(apply(df[ , chars], 2, as.numeric))

#display classes of each column
sapply(df, class)

        a         b         c         d 
"numeric" "numeric"  "factor" "numeric" 

This code made the following changes to the data frame columns:

  • Column a: From character to numeric
  • Column b: From character to numeric
  • Column c: Unchanged (since it was a factor)
  • Column d: Unchanged (since it was already numeric)

By using the and functions, we were able to convert only the character columns to numeric columns and leave all other columns unchanged.

Additional Resources

x