You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Check for .ROW := NULL pattern (delete rows by reference)
1564
+
if ((is.character(lhs) && length(lhs)==1L&&lhs==".ROW") ||
1565
+
(is.name(lhs) && identical(lhs, quote(.ROW)))) {
1566
+
if (!is.null(jsub) &&!identical(jsub, quote(NULL)))
1567
+
stopf(".ROW can only be used with := NULL to delete rows")
1568
+
if (is.null(irows))
1569
+
stopf(".ROW := NULL requires i= condition to specify rows to delete")
1570
+
if (!missingby)
1571
+
stopf(".ROW := NULL does not support 'by' or 'keyby'. To delete rows using by grouping, first compute the row indices (e.g. rows = DT[, .I[cond], by=grp]$V1) and then delete them DT[rows, .ROW := NULL].")
1572
+
.Call(CdeleteRows, x, irows)
1573
+
return(suppPrint(x))
1574
+
}
1562
1575
av= all.vars(jsub,TRUE)
1563
1576
if (!is.atomic(lhs)) stopf("LHS of := must be a symbol, or an atomic vector (column names or positions).")
Copy file name to clipboardExpand all lines: man/assign.Rd
+11Lines changed: 11 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -56,12 +56,16 @@ set(x, i = NULL, j, value)
56
56
DT[i, colC:= mean(colB), by=colA] # update (or add) column called "colC" by reference by group. A major feature of `:=`.
57
57
DT[,`:=`(new1= sum(colB), new2= sum(colC))] # Functional form
58
58
DT[, let(new1= sum(colB), new2= sum(colC))] # New alias for functional form.
59
+
DT[i, .ROW:=NULL] # delete rows by reference.
59
60
}
60
61
61
62
The \code{\link{.Last.updated}} variablecontainsthenumberofrowsupdatedbythemostrecent \code{:=} or \code{set} calls, whichmaybeuseful, forexample, inproductionsettingsfortestingassumptionsaboutthenumberofrowsaffectedbyastatement; see \code{\link{.Last.updated}} fordetails.
Thebindingsofthesevariablesarelockedandattemptingtoassigntothemwillgenerateanerror.Ifyouwishtomanipulate \code{.SD} beforereturningit, takea \code{\link{copy}(.SD)} first (seeFAQ4.5).Using \code{:=} inthe \code{j} of \code{.SD} isreservedforfutureuseas a (tortuously) flexiblewaytoupdate \code{DT} byreferenceby group (evenwhengroupsarenotcontiguousinanadhocby).
@@ -32,6 +34,8 @@
32
34
33
35
\code{.NATURAL} isdefinedas \code{NULL} butitsvalueisnotused.Itsusageis \code{on=.NATURAL} (alternativeof \code{X[on=Y]}) whichjoinstwotablesontheircommoncolumnnames, performinganaturaljoin; see \code{\link{data.table}}'s \code{on} argument for more details.
34
36
37
+
\code{.ROW} is a symbol that can only be used with \code{:= NULL} to delete rows by reference. When you use \code{DT[i, .ROW := NULL]}, the rows matching the \code{i} expression are removed from \code{DT} in-place. This is an efficient way to delete rows without copying the entire data.table. The \code{i} argument is required and \code{by}/\code{keyby} are not supported. After deletion, any keys and indices on \code{DT} are cleared. See \code{\link{:=}} for more on reference semantics.
38
+
35
39
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
40
37
41
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.
@@ -72,5 +76,12 @@ DT[, .(min(.SD[,-1])), by=.I]
72
76
# Do not expect this to correctly append the value of .BY in each group; copy(.BY) will work.
0 commit comments