Using data.table
The data.table library describes itself as an enhanced version of the data.frames in R. Using only base R, it is not easy to group data, for example. There are other small enhancements, such as not converting strings to factors during data import and in the visualization of printing datasets on R’s console.
The syntax for this library is very similar to data.frames
, as you may have already seen during this chapter, but it is formally presented here:
Basic syntax DT[i, j, by]
i
is for the row selection or a condition for the rows to be displayedj
is for selecting variables or calculating a statistic based on themby
is used for grouping variables
Before using the syntax for data.table, it is necessary to make sure that the object is the correct type. That can be done using type(object)
. Conversion to a data.table
object can be done using as.data.table(object)
.
Consider the following code snippet:
# Syntax dt[dt$age > 50...