Skip to content

Join vignette - #6478

Merged
tdhock merged 16 commits into
Rdatatable:masterfrom
AngelFelizR:join_vignette
Oct 7, 2024
Merged

tdhock merged 16 commits into
Rdatatable:masterfrom
AngelFelizR:join_vignette

Conversation

@AngelFelizR

Copy link
Copy Markdown
Contributor

This pull request is my solution to issue #2181

Comment thread vignettes/datatable-joins.Rmd Outdated
)
```

In this vignette you will learn how to perform any join operation using next resources available in the `data.table` syntax.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete "next"

set.seed(5415)

ProductSales = data.table(
id = 1:10,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here there are ten rows which may be overly complex for a first demonstration example
could it be reduced to two rows?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be challenging, as I would need to modify the examples below, since they are related. However, I do not expect the user to pay much attention to that detail.

We can consider avoiding the table display.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is ok to keep all ten rows if necessary, but smaller is better in terms of learning examples usually.

Comment thread vignettes/datatable-joins.Rmd Outdated
```
x[i, on, nomatch]
| | | |
| | | ----> If NULL only returns rows linked in x and i tables

@tdhock tdhock Sep 6, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe use \ and _ instead of - ?

|
\__

Comment thread vignettes/datatable-joins.Rmd Outdated
| | | ----> If NULL only returns rows linked in x and i tables
| | ----> a character vector o list defining match logict
| ----> principal data.table, list or data.frame
----> secundary data.table

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

secondary

Comment thread vignettes/datatable-joins.Rmd Outdated
| | | |
| | | ----> If NULL only returns rows linked in x and i tables
| | ----> a character vector o list defining match logict
| ----> principal data.table, list or data.frame

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

primary instead of principal?

also where do these names, principal/secondary come from? I don't see them in ?data.table so I would suggest to keep the standard/docmented names, i and x

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Toby, thanks for pointing this out, I will change it from principal to primary.

I am trying to keep using the documented reference.

I just thought it could be useful for a new user to understand that the i table is more important in defining the number of rows to return, regardless of whether they are trying to make a left or right join.

Comment thread vignettes/datatable-joins.Rmd Outdated
----> secundary data.table
```

An important difference between the regular `data.table` syntax is that _the only argument you can pass by position is the `i` argument_, the rest as you will see are going to be passed by name, so **feel free to change the argument order any time using the argument names** if it seems more convenient.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not true. In R you can pass any argument by name or position. on is 15th so it is difficult, but possible.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your point.

Here is the new text

Please keep in mind that the standard argument order in data.table is dt[i, j, by]. For join operations, it is recommended to pass the on and nomatch arguments by name to avoid using j and by when they are not needed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand "to avoid using j and by when they are not needed." can you please clarify?

Here is my attempt to clarify. Is this similar to what you meant? "For join operations using the data table square brackets, the first argument (i=table or names) is often specified as a positional argument, and the on and nomatch arguments are often specified by name, for example: dt1[dt2, on="variable", nomatch=0L]."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is a demonstration that it is possible to use on argument by position instead of name (I don't think this is a great idea to do in practice though haha)

> dt1=data.table(x=1:2, y=3:4)
> dt2=data.table(x=2)
> dt1[dt2,,,,,,,,,,,,,,"x"]
       x     y
   <int> <int>
1:     2     4

Comment thread vignettes/datatable-joins.Rmd Outdated

### 3.1. Right join

Use this method if you need to combine columns from 2 tables based on one or more references but ***keeping all rows present in the table located on the right***.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change right to i ? or at least mention i in addition to right

@tdhock tdhock Sep 6, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "table in the square brackets" or "i argument" should be mentioned in addition to "table located on the right"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change right to i ? or at least mention i in addition to right

I think it can be confusing as the left join it's also an i join, just exchanging the position of each table

Comment thread vignettes/datatable-joins.Rmd Outdated

```{r}
Products[ProductReceived,
on = c("id" = "product_id")]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"id" quotes not necessary

Comment thread vignettes/datatable-joins.Rmd Outdated
Our recommendation is to use the second alternative if possible, as it is **faster** and uses **less memory** than the first one.


##### 3.1.3.1. Managing shared column Names with the j argument

@tdhock tdhock Sep 6, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

four levels of section numbering (3.1.3.1) is difficult to understand. can it be kept to 2 or 3 please? (3.1) or (3.1.3)

Comment thread vignettes/datatable-joins.Rmd Outdated
Comment on lines +257 to +261
dt1 =
ProductReceived[Products,
on = c("product_id" = "id"),
by = .EACHI,
j = .(total_value_received = sum(price * count))]

@tdhock tdhock Sep 6, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please consider alternative whitespace formatting below.
when each argument of square brackets is on its own line, each argument can be commented easily

dt1 = ProductReceived[
  Products,
  on = c("product_id" = "id"),
  by = .EACHI,
  j = .(total_value_received  = sum(price * count))
]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this format.

Comment thread vignettes/datatable-joins.Rmd Outdated

```r
DT[ ...
][ ...

@tdhock tdhock Sep 6, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DT[
  ...
][
  ...
][
  ...
]

Comment thread vignettes/datatable-joins.Rmd Outdated
]
```

So far, if after applying all that operations **we want to join new columns without removing any row**, we would need to stop the chaining process, save a temporal table and later apply the join operation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

temporal -> temporary

Comment thread vignettes/datatable-joins.Rmd Outdated
allow.cartesian = TRUE]
```

> `allow.cartesian` is defaulted to FALSE as this joins can can lead to a very large number of rows in the result.For example, if Table A has 100 rows and Table B has 50 rows, their Cartesian product would result in 5000 rows (100 * 50). This can quickly become memory-intensive for large datasets.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add space after period (.)

@tdhock tdhock left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the highly significant contribution!
looks great overall, I left a few comments to think about.

@ChristianWia

Copy link
Copy Markdown
Contributor

is this vignette candidate too for translations ?

@AngelFelizR

Copy link
Copy Markdown
Contributor Author

After getting the approval, I can make a version in Spanish as it is my native language.

@tdhock

tdhock commented Oct 4, 2024

Copy link
Copy Markdown
Member

Hi @AngelFelizR this is a highly non-trivial contribution of documentation, so can you please add your name to DESCRIPTION as contributor? After that I will merge. Thanks for your revisions!

@AngelFelizR

Copy link
Copy Markdown
Contributor Author

Hi @AngelFelizR this is a highly non-trivial contribution of documentation, so can you please add your name to DESCRIPTION as contributor? After that I will merge. Thanks for your revisions!

Thanks Toby

@tdhock

tdhock commented Oct 7, 2024

Copy link
Copy Markdown
Member

great thanks!

@codecov

codecov Bot commented Oct 7, 2024 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.62%. Comparing base (036fb48) to head (2579f85).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6478   +/-   ##
=======================================
  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
tdhock merged commit dea1e5d into Rdatatable:master Oct 7, 2024
@tdhock

tdhock commented Oct 7, 2024

Copy link
Copy Markdown
Member

also I invited you as a project member, so after you accept, please create new branches/PRs under Rdatatable org, instead of your fork.

@AngelFelizR
AngelFelizR deleted the join_vignette branch October 9, 2024 05:07
@tdhock tdhock mentioned this pull request Nov 15, 2024
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.

4 participants