Convert Data Frame Column to Numeric in R Last Updated : 07 May, 2025 Comments Improve Suggest changes Like Article Like Report In R, converting DataFrame columns to numeric is a common and important step in data preprocessing, particularly for statistical modeling and analysis. Below are effective methods to perform this conversion using base R and the readr package.Method 1: Convert One Column to NumericStart by checking the column to ensure it is in a format that can be converted. If it is factor or character, change it to character with as.character(), and then to numeric with as.numeric(). Employ sapply() to check column classes. R df <- data.frame( x1 = as.factor(c(1, 5, 8, 2)), x2 = c(3, 2, 5, 2), x3 = c(2, 7, 1, 2) ) # Check current classes sapply(df, class) # Convert x1 to numeric df$x1 <- as.numeric(as.character(df$x1)) sapply(df, class) Output: x1 x2 x31 1 3 22 5 2 73 8 5 14 2 2 2 x1 x2 x3 "factor" "numeric" "numeric" Method 2: Convert Multiple Columns to NumericTo convert multiple columns to numeric, give their indices, then apply() to change each to character and then numeric. Apply sapply() to verify the changes. R # Column indices to be converted i <- c(2, 3) df[, i] <- apply(df[, i], 2, function(x) as.numeric(as.character(x))) sapply(df, class) Output: x1 x2 x3 "numeric" "numeric" "numeric" Method 3: Using parse_number() from readrThe parse_number() function from the readr package is a useful function to pull numeric values out of character columns. It makes it easy to convert by parsing numbers from strings. The following example uses x1, followed by checking the result. R library(readr) df <- data.frame(x1 = c("1", "5", "8", "2"), # Character column x2 = c(3, 2, 5, 2), x3 = c(2, 7, 1, 2)) # Display original data frame print("Original DataFrame:") print(df) print(sapply(df, class)) # Convert using parse_number df$x1 <- parse_number(df$x1) # Display modified data frame print("DataFrame after conversion:") print(df) # Verify the class of each column print("Column Classes:") print(sapply(df, class)) Output:[1] "Original DataFrame:" x1 x2 x31 1 3 22 5 2 73 8 5 14 2 2 2 x1 x2 x3 "character" "numeric" "numeric" [1] "DataFrame after conversion:" x1 x2 x31 1 3 22 5 2 73 8 5 14 2 2 2[1] "Column Classes:" x1 x2 x3 "numeric" "numeric" "numeric" Comment More infoAdvertise with us Next Article Convert Data Frame Column to Numeric in R S saiganeshakciz Follow Improve Article Tags : R Language R-DataFrame Similar Reads How to Convert Date to Numeric in R? In this article, we will discuss how to convert date to numeric in R Programming Language. Method 1: Using as.numeric() This function is used to convert date into numeric Syntax: as.numeric(date) where the date is the input date. Example: R data = as.POSIXct("1/1/2021 1:05:00 AM", format="%m/%d/%Y % 2 min read Convert Tibble to Data Frame in R Tibbles are a type of data frame in R Programming Language that has an enhanced print method, making it easier to display data. However, in some situations, we may need to convert a tibble to a data frame. So, in this article, we will explore some approaches to convert tibbles to data frames. Tibble 4 min read Convert DataFrame to vector in R In this article, we will discuss how a dataframe can be converted to a vector in R. For the Conversion of dataframe into a vector, we can simply pass the dataframe column name as [[index]]. Approach: We are taking a column in the dataframe and passing it into another variable by the selection method 2 min read Convert dataframe to data.table in R In this article, we will discuss how to convert dataframe to data.table in R Programming Language. data.table is an R package that provides an enhanced version of dataframe. Characteristics of data.table :Â data.table doesnât set or use row namesrow numbers are printed with a : for better readabilit 5 min read Convert Multiple Columns to Numeric Using dplyr In data analysis with R Programming Language, it's common to encounter datasets where certain columns must be converted to numeric type for further study or modeling. In this article, we'll explore how to efficiently convert multiple columns to numeric using the dplyr package in R. Identifying Colum 8 min read Like