Skip to content
Related Articles

Related Articles

data.table vs data.frame in R Programming
  • Last Updated : 20 Jan, 2021

data.table in R is an enhanced version of the data.frame. Due to its speed of execution and the less code to type it became popular in R. The purpose of data.table is to create tabular data same as a data frame but the syntax varies. In the below example let we can see the syntax for the data table:


DataTable = data.table(name = c("a", "b", "c", "d"),
                       id = (7, 0, 3, 4))

In the above example, we use the function data.table() and then we used the function called c() which means concatenation which is used to print data as a series and the names will be of string type so, we used ” ” and id as an integer type so no need to specify in quotes.

data.frame in R is similar to the data table which is used to create tabular data but data table provides a lot more features than the data frame so, generally, all prefer the data.table instead of the data.frame. But the data.frame is also best to use and now let us see the syntax below for the data frame. Similar to the syntax of data.table, data.frame is also obviously the same here, instead of data.table() we use the function data.frame().


# student id
stuid = c(2, 5, 3, 4, 6, 7, 4, 2, 0)  
# student age
age = c(23, 45, 67, 23, 41, 43, 54, 67, 89) 
# sex of the student
sex = c(1, 1, 0, 0, 0, 1, 0, 1, 1)  
# student info
stuinfo = data.frame(empidno, age, sex, status) 

The above example gives us the data of the students in the tabular form. If we observe here, the code for the data.table is less than the code for data.frame and hence, data.table takes less time to compile and gives the output fast so, this makes the data table use widely.

Difference Table



Syntax: data.table()Syntax: data.frame()

data.table is a rewritten form of data.frame in optimized c (or)

data.table inherits from data.frame.

data.frame is the base class in R and it is the default in R.

data.table is used for more complex data structures and for 

big data.

data.frame is used to build small tables and matrices etc.
data.table is very much faster than a spark in many is 20 times slower than data.table

The in-built features such as rolling joins, overlapping range 

makes users sort out the wide range of problems.

data.frame lacks these features but it is good for beginners.

Code efficient (we can able to write less number of lines of 

code in data.table)

We need to write some more lines of code when compared 

to data.table

To convert data.table to data.frame we use : setDF(dt) where
DF = data frame and dt = data table.
To convert data.frame to data.table we use : setDT(df) where
DT = data table and df = data frame.
data.table is widely used due to its advanced features and
speed and memory.
data.frame is also used but not that much of data.table and
it is very good for beginners.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :