Skip to content

Commit 3020933

Browse files
Nj221102nitish jhaMichaelChirico
authored
Clarify Usage of .BY in Special Symbols Documentation (#6193)
* udated documentation of .BY * updated documentation of .BY * improving the example * improve example * Update special-symbols.Rd * Simplify * Refine wording further * no pseudo-code :) --------- Co-authored-by: nitish jha <[email protected]> Co-authored-by: Michael Chirico <[email protected]> Co-authored-by: Michael Chirico <[email protected]>
1 parent b1c6bf3 commit 3020933

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

man/special-symbols.Rd

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
\code{.EACHI} is a symbol passed to \code{by}; i.e. \code{by=.EACHI}, \code{.NATURAL} is a symbol passed to \code{on}; i.e. \code{on=.NATURAL}
1616
}
1717
\details{
18-
The bindings of these variables are locked and attempting to assign to them will generate an error. If you wish to manipulate \code{.SD} before returning it, take a \code{copy(.SD)} first (see FAQ 4.5). Using \code{:=} in the \code{j} of \code{.SD} is reserved for future use as a (tortuously) flexible way to update \code{DT} by reference by group (even when groups are not contiguous in an ad hoc by).
18+
The bindings of these variables are locked and attempting to assign to them will generate an error. If you wish to manipulate \code{.SD} before returning it, take a \code{\link{copy}(.SD)} first (see FAQ 4.5). Using \code{:=} in the \code{j} of \code{.SD} is reserved for future use as a (tortuously) flexible way to update \code{DT} by reference by group (even when groups are not contiguous in an ad hoc by).
1919

2020
These symbols used in \code{j} are defined as follows.
2121

@@ -33,6 +33,8 @@
3333
\code{.NATURAL} is defined as \code{NULL} but its value is not used. Its usage is \code{on=.NATURAL} (alternative of \code{X[on=Y]}) which joins two tables on their common column names, performing a natural join; see \code{\link{data.table}}'s \code{on} argument for more details.
3434
3535
Note that \code{.N} in \code{i} is computed up-front, while that in \code{j} applies \emph{after filtering in \code{i}}. That means that even absent grouping, \code{.N} in \code{i} can be different from \code{.N} in \code{j}. See Examples.
36+
37+
Note also that you should consider these symbols read-only and of limited scope -- internal data.table code might manipulate them in unexpected ways, and as such their bindings are locked. There are subtle ways to wind up with the wrong object, especially when attempting to copy their values outside a grouping context. See examples; when in doubt, \code{copy()} is your friend.
3638
}
3739
\seealso{
3840
\code{\link{data.table}}, \code{\link{:=}}, \code{\link{set}}, \code{\link{datatable-optimize}}
@@ -66,5 +68,9 @@ DT[{cat(sprintf('in i, .N is \%d\n', .N)); a < .N/2},
6668
# .I can be different in j and by, enabling rowwise operations in by
6769
DT[, .(.I, min(.SD[,-1]))]
6870
DT[, .(min(.SD[,-1])), by=.I]
71+
72+
# Do not expect this to correctly append the value of .BY in each group; copy(.BY) will work.
73+
by_tracker = list()
74+
DT[, { append(by_tracker, .BY); sum(v) }, by=x]
6975
}
7076
\keyword{ data }

0 commit comments

Comments
 (0)