The data.table package

data.table is a revolutionary package in R because it uses a new approach to data processing that results in a much faster execution time. However, it has a drawback that it uses a syntax that, although easy to understand, differs considerably from the normal R syntax.

Mainly, every operation in a data table is done inside brackets that normally refer to dimensions in arrays or data frames:

data.table.object[operations over rows, operations over columns, by]

In the preceding code snippet, by is optional. The columns can be selected either by name or column index. However, for this last option, the with=FALSE argument has to be added:

> data(iris)
> iris <- data.table(iris)
> iris[,2,with=F]
Sepal.Width
1: 3.5
2: 3.0
3: 3.2
4: 3.1
5: 3.6
---
146: 3.0
147: 2.5
148: 3.0
149: 3.4
150: 3.0

Data table objects are also printed differently. Data table also has the functionality of adding new variables to the object without any need to rename it using the := operator:

> names(iris)
[1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
> iris[,ratio:=Sepal.Length/Sepal.Width]
> names(iris)
[1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
[6] "ratio"

Note that there is no variable assignment during the creation of the variable ratio. The := operator creates the variable implicitly in the same object, as follows:

> iris[Sepal.Length >= 5.1, median(Petal.Width), by=list(Species)]
Species V1
1: setosa 0.2
2: versicolor 1.3
3: virginica 2.0

In this example, the median of Petal Width by species with a Sepal Length over 5.1 is calculated.

Definitely, a good use of this can improve the performance of data processing tasks, and this can be crucial for dashboards to provide a fast response in real time. However, this section is just a very general overview of data table as it is not the aim of the book to go deeper into these topics. For this reason, the reader is highly encouraged to dive deep into data table to exploit all its potential.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset