Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update datatable-intro.Rmd #6558

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

gurbuxanink
Copy link

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.

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.
@tdeenes
Copy link
Member

tdeenes commented Oct 6, 2024

note that this is not exactly true, data.frames can hold list elements:

datfr <- data.frame(idx = 1:3)
datfr$lst <- lapply(datfr$idx, seq_len)
str(datfr)
# 'data.frame':	3 obs. of  2 variables:
#  $ idx: int  1 2 3
#  $ lst:List of 3
#   ..$ : int 1
#   ..$ : int  1 2
#   ..$ : int  1 2 3

Having said that, of course it is much more convenient to use data.tables for working with this kind of objects. E.g.:

lst <- lapply(1:3, seq_len)
## this works
data.table(idx = seq_along(lst), lst = lst)
## this fails
data.frame(idx = seq_along(lst), lst = lst)

@tdhock
Copy link
Member

tdhock commented Oct 9, 2024

please clarify your proposed addition by mentioning that it is possible in frame but requires a bit more typing, since you have to wrap lists with I()

> data.table(L=list(1:2))
        L
   <list>
1:    1,2
> data.frame(L=list(1:2))
  X1.2
1    1
2    2
> data.frame(L=I(list(1:2)))
     L
1 1, 2

Illustrate creation and storage of models as lists within data.table  Provide a counter-example using data.frames where code has more lines and is more difficult to understand.
Copy link

codecov bot commented Oct 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.62%. Comparing base (bb9faf6) to head (03de9eb).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6558   +/-   ##
=======================================
  Coverage   98.62%   98.62%           
=======================================
  Files          79       79           
  Lines       14450    14450           
=======================================
  Hits        14251    14251           
  Misses        199      199           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tdhock
Copy link
Member

tdhock commented Oct 10, 2024

that looks better thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants