-
Notifications
You must be signed in to change notification settings - Fork 1k
Clarify $ vs [, col] behavior in docs (#7369) #7381
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
579566c
updated doc
venom1204 891f505
removed library
venom1204 84452da
Update vignettes/datatable-reference-semantics.Rmd
venom1204 8aef2be
Update vignettes/datatable-reference-semantics.Rmd
venom1204 ef07281
Update vignettes/datatable-reference-semantics.Rmd
venom1204 7eb0c02
Update vignettes/datatable-faq.Rmd
venom1204 2c0f0ff
updated
venom1204 f685b77
Merge branch 'issue7369' of https://github.com/Rdatatable/data.table …
venom1204 c43f3d2
Merge branch 'master' into issue7369
ben-schwen d10831e
wording
ben-schwen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -363,6 +363,37 @@ However we could improve this functionality further by *shallow* copying instead | |
| ## DT_n doesn't get updated | ||
| DT_n | ||
| ``` | ||
| ### c) Selecting columns: `$` vs `[, col]` | ||
|
|
||
| When selecting a column as a vector, `DT$col` and `DT[, col]` have a key difference: `DT$col` may return a reference to the data, while `DT[, col]` always returns a copy. | ||
|
|
||
| This means a variable created with `$` can change if the `data.table` is modified later, which can be unexpected. A single example demonstrates this behavior and how `copy()` provides a solution: | ||
|
|
||
venom1204 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ```{r} | ||
| library(data.table) | ||
|
||
|
|
||
| DT = data.table(a = 1:3) | ||
|
|
||
| # Create three variables using the different methods | ||
| x_ref = DT$a # 1. By reference with $ | ||
| y_cpy = DT[, a] # 2. By copy with [] | ||
| z_cpy = copy(DT$a) # 3. Forced copy with copy() | ||
|
|
||
| # Now, modify the original data.table by reference | ||
| DT[, a := a + 10L] | ||
|
|
||
| # Check the variables: | ||
| x_ref | ||
| #> [1] 11 12 13 | ||
| y_cpy | ||
| #> [1] 1 2 3 | ||
| z_cpy | ||
| #> [1] 1 2 3 | ||
venom1204 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| To select a single column as a vector, remember: | ||
| - `DT[, mycol]` is safer as it always returns a new, independent copy. | ||
| - `DT$mycol` is fast but may return a reference. Use `copy(DT$mycol)` to guarantee independence. | ||
venom1204 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Summary | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.