-
Notifications
You must be signed in to change notification settings - Fork 968
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
Clarify Usage of .BY
in Special Symbols Documentation
#6193
base: master
Are you sure you want to change the base?
Conversation
man/special-symbols.Rd
Outdated
@@ -66,5 +68,25 @@ DT[{cat(sprintf('in i, .N is \%d\n', .N)); a < .N/2}, | |||
# .I can be different in j and by, enabling rowwise operations in by | |||
DT[, .(.I, min(.SD[,-1]))] | |||
DT[, .(min(.SD[,-1])), by=.I] | |||
|
|||
#These examples illustrate the importance of using copy(.BY) when capturing .BY values in a list outside the data.table context to avoid unexpected behavior. | |||
dt <- data.table(l = sample(letters[1:5], 100, replace = TRUE), b = rnorm(100)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi, this is a good start, but how about a simpler example, with fewer than 100 rows?
how about 4 rows, data.table(letter=c('a','a','b','b'),number=1:4)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done :)
Hi @Nj221102, FYI, there's no need to do the "Update" step for each individual PR every time |
Sure will keep that in mind from now on , sorry for the inconvenience caused |
@tdhock plz check this out as well. |
@@ -66,5 +68,25 @@ DT[{cat(sprintf('in i, .N is \%d\n', .N)); a < .N/2}, | |||
# .I can be different in j and by, enabling rowwise operations in by | |||
DT[, .(.I, min(.SD[,-1]))] | |||
DT[, .(min(.SD[,-1])), by=.I] | |||
|
|||
# These examples illustrate the importance of using copy(.BY) when capturing .BY values in a list outside the data.table context to avoid unexpected behavior. | |||
dt <- data.table(l = letters[1:5], b = rnorm(5)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again please use simpler data (1:4 instead of rnorm) as I mentioned in my previous comment
byg <<- append(byg, .BY) # Incorrect: Missing copy(.BY) | ||
mean(b) | ||
}, by = l] | ||
str(byg) # All elements of 'byg' will contain the .BY value of the last group | ||
|
||
## Correct usage | ||
byg <- list() | ||
dt[, m := { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these examples are perhaps too long.
If I understand correctly, the important different is .BY versus copy(.BY) right?
So can you please delete as much other code (:= append, mean) as possible to make the example simpler? ideally one line instead of 6 for both incorrect/correct usage
closes #5389
Description:
This PR updates the
special-symbols
documentation to make it clear how to use.BY
correctly indata.table
. The main goal is to help users understand how to avoid issues when using.BY
outside of its immediate context, as discussed in GitHub Issue #5389.Changes:
details
section to include advice on usingcopy(.BY)
when extracting.BY
outside its immediate context.copy(.BY)
.