# How to Convert a Character to Numeric in R

**Basic R Syntax:**

x_num <- as.numeric(x) |

x_num <- as.numeric(x)

Do you need more explanations? In the following article, I’ll provide you with all important information for the **conversion of character vectors to numeric in R**.

## Example: Convert Character to Numeric in R

Before we can dive into the transformation of a character variable to numeric, we need to create an example character in R. Consider the following vector:

set.seed(55555) # Set seed x <- as.character(sample(c(2, 5, 7, 8), 50, replace = TRUE)) # Example character vector |

set.seed(55555) # Set seed x <- as.character(sample(c(2, 5, 7, 8), 50, replace = TRUE)) # Example character vector

Our string consists of the four character values 2, 5, 7 & 8:

`x # Print example vector to R console` |

x # Print example vector to R console

**Graphic 1: Example Character String Printed to the RStudio Console**

Now, we can continue with the important part – How to convert this character string to numeric?

No Problem:

x_num <- as.numeric(x) # Convert string to numeric in R x_num # Print converted x to the console # 8 7 5 8 2 5 2 5 2 7 7 7 7... |

x_num <- as.numeric(x) # Convert string to numeric in R x_num # Print converted x to the console # 8 7 5 8 2 5 2 5 2 7 7 7 7...

## Convert All Characters of a Data Frame to Numeric

As you have seen, to convert a vector or variable with the character class to numeric is no problem. However, sometimes it makes sense to change all character columns of a **data frame or matrix** to numeric.

Consider the following R data.frame:

x1 <- c("5", "2", "7", "5") # Character x2 <- c("77", "23", "84", "11") # Another character x3 <- as.factor(c("4", "1", "1", "8")) # Factor x4 <- c(3, 3, 9, 7) # Numeric data <- data.frame(x1, x2, x3, x4, # Create data frame stringsAsFactors = FALSE) sapply(data, class) # Print classes of all colums # x1 x2 x3 x4 # "character" "character" "factor" "numeric" |

x1 <- c("5", "2", "7", "5") # Character x2 <- c("77", "23", "84", "11") # Another character x3 <- as.factor(c("4", "1", "1", "8")) # Factor x4 <- c(3, 3, 9, 7) # Numeric data <- data.frame(x1, x2, x3, x4, # Create data frame stringsAsFactors = FALSE) sapply(data, class) # Print classes of all colums # x1 x2 x3 x4 # "character" "character" "factor" "numeric"

**Table 1: Example Data Frame with Different Variable Classes**

With the following R code, you are able to recode all variables – **no matter which variable class** – of a data frame to numeric:

data_num <- as.data.frame(apply(data, 2, as.numeric)) # Convert all variable types to numeric sapply(data_num, class) # Print classes of all colums # x1 x2 x3 x4 # "numeric" "numeric" "numeric" "numeric" |

data_num <- as.data.frame(apply(data, 2, as.numeric)) # Convert all variable types to numeric sapply(data_num, class) # Print classes of all colums # x1 x2 x3 x4 # "numeric" "numeric" "numeric" "numeric"

However, in many situations it is better to **convert only character columns to numeric** (i.e. not column X3, since this column should be kept as factor). You could do that with the following code in R:

char_columns <- sapply(data, is.character) # Identify character columns data_chars_as_num <- data # Replicate data data_chars_as_num[ , char_columns] <- as.data.frame( # Recode characters as numeric apply(data_chars_as_num[ , char_columns], 2, as.numeric)) sapply(data_chars_as_num, class) # Print classes of all colums # x1 x2 x3 x4 # "numeric" "numeric" "factor" "numeric" |

char_columns <- sapply(data, is.character) # Identify character columns data_chars_as_num <- data # Replicate data data_chars_as_num[ , char_columns] <- as.data.frame( # Recode characters as numeric apply(data_chars_as_num[ , char_columns], 2, as.numeric)) sapply(data_chars_as_num, class) # Print classes of all colums # x1 x2 x3 x4 # "numeric" "numeric" "factor" "numeric"

## Example Video: How to Change Variable Types

Further examples needed? Have a look at the following R Programming tutorial of the YouTube Channel LearnR. The speaker discusses different data transformations from one data class to another.

## Further Reading

- How to Convert Factor to Numeric in R
- Convert a Data Frame Column to Numeric
- The R Programming Language

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R