Using the data.table package setDT() function in R, we can convert data frame to a data table. The data.table package in R provides an enhanced version of a data frame. setDT() function converts the data frame to the data table.

setDT() syntax:

setDT(x, keep.rownames=FALSE, key=NULL, check.names=FALSE)

where,
x = data frame, data table or named or unnamed list

In this tutorial, we will discuss how to convert a data frame to a data table using the setDT() function and as.data.table() function in R.

Convert Data Frame to Data Table using setDT

Use the setDT() function of data.table package to convert a data frame to a data table object in R.

Let’s practice with an example to understand converting a data frame to a data table.

Create a data frame in R using the data.frame function.

# Create a data frame
sales_data <- data.frame(
  id = c(1,2,3,4,5,6),
  name = c("Ebook","Audio","Video","Course","Book","Software"),
  revenue = c(45500,53000,55200,13650,33400,13800)
)
# Print the data frame
sales_data
# Check the class of the object
class(sales_data)

In the above R code, we create a data frame as sales_data and print the data frame and its class.

The output of the above R code is:

  id     name revenue
1  1    Ebook   45500
2  2    Audio   53000
3  3    Video   55200
4  4   Course   13650
5  5     Book   33400
6  6 Software   13800

[1] "data.frame"

Now, let’s convert a data frame to a data table using setDT in R.

# Import a data.table library
library(data.table)
# Convert data frame to data table
setDT(sales_data)
# Prints the sales_data object
sales_data
# Check the class of the object
class(sales_data)

In the above R code, we first import data.table library. Using the setDT() in R, it converts the data frame to the data table, print data table and its class.

The output of the above R code is:

   id     name revenue
1:  1    Ebook   45500
2:  2    Audio   53000
3:  3    Video   55200
4:  4   Course   13650
5:  5     Book   33400
6:  6 Software   13800

[1] "data.table" "data.frame"

From the above output of data frame conversion to a data table, we understand that,

  • The data.table doesn’t set or use row names.
  • Unlike data.frames, columns of character type are never converted to factors by default.
  • Row numbers are printed with: to distinguish it from the first column.

Convert the Data Frame to Data Table using as.data.table

Using the as.data.table() function in R, we can convert a data frame to a data table.

as.data.table syntax:

as.data.table(x, keep.rownames=FALSE, ...)

where,

x = R object

Let’s understand with an example to convert a data frame to a data table using as.data.table() function in R.

Create a data frame in R using the data.frame function.

# Create a data frame
emp_info <- data.frame(
  id = c(1,2,3,4,5,6),
  name = c("Henry","Gary","Sam","Julie","Kim","Chris"),
  age = c(28,24,29,25,23,24),
  salary = c(4500,4000,5500,3650,3400,3800)
)
# Print data frame
emp_info
# Print the class of R object
class(emp_info)

The above R code, create a data frame and print the data frame. It prints the class of R object as data.frame.

The output of the above R code is:

  id  name age salary
1  1 Henry  28   4500
2  2  Gary  24   4000
3  3   Sam  29   5500
4  4 Julie  25   3650
5  5   Kim  23   3400
6  6 Chris  24   3800


[1] "data.frame"

Use the as.data.table() function in R to convert a data frame to a data table in R.

# Convert data frame to data table in R
df <- as.data.table(emp_info)
# Print the data frame 
df
# Print the class of R object
class(df)

In the above R code, we have used as.data.table() function in R and passes data frame as an input parameter. It converts the data frame to the data table and prints the class of R object as data.table and data.frame.

The output of the above R code is:

   id  name age salary
1:  1 Henry  28   4500
2:  2  Gary  24   4000
3:  3   Sam  29   5500
4:  4 Julie  25   3650
5:  5   Kim  23   3400
6:  6 Chris  24   3800

[1] "data.table" "data.frame"

Conclusion

I hope the above article on using the setDT() in R to convert a data frame to a data table object is helpful to you.

The setDT() function of data.table requires data.table package to be installed on your system.

We can used as.data.table() function to convert a data frame to a data table object.