R Convert data.table Columns to Numeric/Character/Factor (2 Examples)

In this article, I’ll show how to convert the data types of the variables of a data.table in the R programming language.

Preparing the Examples

install.packages("data.table")                                # Install & load data.table
library("data.table")
data(iris)                                                    # Load iris data
iris_dt <- as.data.table(iris)                                # Convert iris to data table
head(iris_dt)                                                 # Head of iris data table
#    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1:          5.1         3.5          1.4         0.2  setosa
# 2:          4.9         3.0          1.4         0.2  setosa
# 3:          4.7         3.2          1.3         0.2  setosa
# 4:          4.6         3.1          1.5         0.2  setosa
# 5:          5.0         3.6          1.4         0.2  setosa
# 6:          5.4         3.9          1.7         0.4  setosa
sapply(iris_dt, class)                                        # Returning classes of all variables
# Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#    "numeric"    "numeric"    "numeric"    "numeric"     "factor"

Example 1:

iris_dt_1col <- iris_dt[ , Species := as.character(Species)]  # Converting one column to character
sapply(data_new1, class)                                      # Returning classes of all variables
#          x1          x2          x3 
# "character" "character" "character"

Example 2:

new_cols <- c("Sepal.Length", "Petal.Length")                 # Define vector of columns
iris_dt_all <- iris_dt                                        # Replicating data table
iris_dt_all[ ,                                                # Modifying classes of particular variables
           (new_cols) := lapply(.SD, as.factor),
           .SDcols = new_cols]
sapply(iris_dt_all, class)                                    # Returning classes of all variables
#      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#   1:          5.1         3.5          1.4         0.2    setosa
#   2:          4.9         3.0          1.4         0.2    setosa
#   3:          4.7         3.2          1.3         0.2    setosa
#   4:          4.6         3.1          1.5         0.2    setosa
#   5:            5         3.6          1.4         0.2    setosa
#  ---                                                            
# 146:          6.7         3.0          5.2         2.3 virginica
# 147:          6.3         2.5            5         1.9 virginica
# 148:          6.5         3.0          5.2         2.0 virginica
# 149:          6.2         3.4          5.4         2.3 virginica
# 150:          5.9         3.0          5.1         1.8 virginica

Further Resources

Have a look at the following R tutorials. They illustrate similar topics as this post:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top