How to convert a data frame to data.table in R?


Since operations with data.table are sometimes faster than the data frames, we might want to convert a data frame to a data.table object. The main difference between data frame and data.table is that data frame is available in the base R but to use data.table we have to install the package data.table. We can do this with the help setDT function in the data.table package.

Example

Consider the below data frame −

> set.seed(1)
> x1<-rnorm(20,0.5)
> x2<-rnorm(20,0.8)
> x3<-rpois(20,2)
> x4<-rpois(20,5)
> x5<-runif(20,5,10)
> df<-data.frame(x1,x2,x3,x4,x5)
> df
x1 x2 x3 x4 x5
1  -0.1264538  1.7189774 2 6 9.959193
2   0.6836433  1.5821363 3 4 7.477968
3  -0.3356286  0.8745650 1 4 7.421748
4   2.0952808 -1.1893517 1 11 5.867212
5   0.8295078  1.4198257 3 6 8.774105
6  -0.3204684  0.7438713 1 3 7.269477
7   0.9874291  0.6442045 3 3 7.555849
8   1.2383247 -0.6707524 0 5 6.037726
9   1.0757814  0.3218499 1 8 6.143291
10  0.1946116  1.2179416 1 5 7.978560
11  2.0117812  2.1586796 1 10 7.874361
12  0.8898432  0.6972123 0 6 5.385322
13 -0.1212406  1.1876716 2 4 5.177703
14 -1.7146999  0.7461950 4 4 8.213977
15  1.6249309 -0.5770596 3 3 9.643076
16  0.4550664  0.3850054 3 1 7.990462
17  0.4838097  0.4057100 2 6 7.804504
18  1.4438362  0.7406866 2 2 7.630139
19  1.3212212  1.9000254 3 5 9.925476
20  1.0939013  1.5631757 2 6 7.538209

Loading data.table package −

> library(data.table)

Converting data frame df to data.table?

> setDT(df)

Checking whether df is a data.table or not −

> is.data.table(df)
[1] TRUE

Let’s have a look at one more example −

> y1<-letters[1:20]
> y2<-rep(c("Group1","Group2","Group3","Group4"),times=5)
> y3<-1:20
> df_new<-data.frame(y1,y2,y3)
> df_new
   y1  y2    y3
1  a Group1  1
2  b Group2  2
3  c Group3  3
4  d Group4  4
5  e Group1  5
6  f Group2  6
7  g Group3  7
8  h Group4  8
9  i Group1  9
10 j Group2 10
11 k Group3 11
12 l Group4 12
13 m Group1 13
14 n Group2 14
15 o Group3 15
16 p Group4 16
17 q Group1 17
18 r Group2 18
19 s Group3 19
20 t Group4 20
> setDT(df_new)
> is.data.table(df_new)
[1] TRUE

Updated on: 11-Aug-2020

835 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements