# Convert Data Frame Column to Numeric in R (2 Examples) | Change Factor, Character & Integer

In this R tutorial, I’ll explain how to **convert a data frame column to numeric** in R. No matter if you need to **change the class of factors, characters, or integers**, this tutorial will show you how to do it.

The article is structured as follows:

- Creation of Example Data in R
- Convert One Column to Numeric (Example 1)
- Convert Multiple Columns to Numeric (Example 2)
- Further Resources for Handling Data Types

Let’s dive right in!

## Create Example Data

First we need to create some data in R that we can use in the examples later on:

data <- data.frame(x1 = c(1, 5, 8, 2), # Create example data frame x2 = c(3, 2, 5, 2), x3 = c(2, 7, 1, 2)) data$x1 <- as.factor(data$x1) # First column is a factor data$x2 <- as.character(data$x2) # Second column is a character data$x3 <- as.integer(data$x3) # Third column is an integer data # Print data to RStudio console |

data <- data.frame(x1 = c(1, 5, 8, 2), # Create example data frame x2 = c(3, 2, 5, 2), x3 = c(2, 7, 1, 2)) data$x1 <- as.factor(data$x1) # First column is a factor data$x2 <- as.character(data$x2) # Second column is a character data$x3 <- as.integer(data$x3) # Third column is an integer data # Print data to RStudio console

You can see the structure of our example data frame in Table 1. The data contains three columns: a factor variable, a character variable, and an integer variable.

**Table 1: Example Data Frame with Factor, Character & Integer Variables.**

We can check the class of each column of our data table with the sapply function:

sapply(data, class) # Get classes of all columns # x1 x2 x3 # "factor" "character" "integer" |

sapply(data, class) # Get classes of all columns # x1 x2 x3 # "factor" "character" "integer"

The data is set up, so let’s move on to the examples…

## Example 1: Convert One Variable of Data Frame to Numeric

In the first example I’m going to convert only one variable to numeric. For this task, we can use the following R code:

data$x1 <- as.numeric(as.character(data$x1)) # Convert one variable to numeric |

data$x1 <- as.numeric(as.character(data$x1)) # Convert one variable to numeric

**Note:** The previous code converts our factor variable to character first and then it converts the character to numeric. This is important in order to retain the values (i.e. the numbers) of the factor variable. You can learn more about that in this tutorial.

However, let’s check the classes of our columns again to see how our data has changed:

sapply(data, class) # Get classes of all columns # x1 x2 x3 # "numeric" "character" "integer" |

sapply(data, class) # Get classes of all columns # x1 x2 x3 # "numeric" "character" "integer"

As we wanted: The factor column was converted to numeric.

## Example 2: Change Multiple Columns to Numeric

In Example 1 we used the as.numeric and the as.character functions to modify one variable of our example data. However, when we want to change several variables to numeric simultaneously, the approach of Example 1 might be too slow (i.e. too much programming). In this example, I’m therefore going to show you how to **change as many columns as you want** at the same time.

First, we need to specify which columns we want to modify. In this example, we are converting columns 2 and 3 (i.e. the character string and the integer):

i <- c(2, 3) # Specify columns you want to change |

i <- c(2, 3) # Specify columns you want to change

We can now use the apply function to change columns 2 and 3 to numeric:

data[ , i] <- apply(data[ , i], 2, # Specify own function within apply function(x) as.numeric(as.character(x))) |

data[ , i] <- apply(data[ , i], 2, # Specify own function within apply function(x) as.numeric(as.character(x)))

Let’s check the classes of the variables of our data frame:

sapply(data, class) # Get classes of all columns # x1 x2 x3 # "numeric" "numeric" "numeric" |

sapply(data, class) # Get classes of all columns # x1 x2 x3 # "numeric" "numeric" "numeric"

The whole data frame was converted to numeric!

## Further Resources

Converting variable classes in R is a complex topic. I have therefore listed some additional resources about the Modification of R data classes in the following.

If you want to learn more about the basic data types in R, I can recommend the following video of the Data Camp YouTube channel:

Also, you could have a look at the following R tutorials of this homepage:

- How to Convert Factor to Numeric
- How to Convert Character to Numeric
- List of Useful R Functions
- The R Programming Language

I hope you liked this tutorial! Let me know in the comments if you have any further questions and of cause I am also happy about general feedback.

### 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