Skip to content

Commit

Permalink
Update datatable-intro.Rmd
Browse files Browse the repository at this point in the history
A powerful feature of data.table is the ability to store lists as elements (a facility not available in data.frame).  Here is a simple illustration of storing a fitted model as data.table element.

When statistical models are fit to different groups within a data.table, these models can be stored in a result data.table.  Now this resulting data.table can be used to access fitted models.
  • Loading branch information
gurbuxanink authored Oct 5, 2024
1 parent 036fb48 commit 81df375
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions vignettes/datatable-intro.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,17 @@ DT[, print(list(c(a,b))), by = ID] # (2)

In (1), for each group, a vector is returned, with length = 6,4,2 here. However, (2) returns a list of length 1 for each group, with its first element holding vectors of length 6,4,2. Therefore, (1) results in a length of ` 6+4+2 = `r 6+4+2``, whereas (2) returns `1+1+1=`r 1+1+1``.

Flexibility of j allows us to store any list object as an element of data.table (a facility not available in data.frames). For example, when statistical models are fit to groups, these models can be stored in a data.table.

```{r}
## Do long distance flights cover up departure delay more than short distance flights?
## Does cover up vary by month?
flights[, `:=`(makeup = dep_delay - arr_delay)]
makeup.models <- flights[, .(fit = list(lm(makeup ~ distance))), by = .(month)]
makeup.models[, .(coefdist = coef(fit[[1]])[2], rsq = summary(fit[[1]])$r.squared), by = .(month)]
```

## Summary

The general form of `data.table` syntax is:
Expand Down

0 comments on commit 81df375

Please sign in to comment.