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
NB: I did not just assign b <- a but b <- a[]. According to the documentation (see below) I'd expect b to be independent from a, and changes not reflect back to a. On a first glance that seems to be the case:
b[1, 1] <- 4
b
a
all(b==a)
However when using := to assign the new value that logic seems broken:
b <- a[]
b
b[1, nr:=4]
b
a
all(b==a)
What am I missing from the documentation, which says that the subsetting operation returns a newdata.table, which then in turn is updated by reference using the := operator?
The effect is the same when creating b as b <- a[TRUE]. However when using b <- a[1:.N] then both objects seem to be separate entities as I'd expect.
See Note section of :=:
DT[a > 4, b := c] is different from DT[a > 4][, b := c]. The first expression updates (or adds) column b with the value c on those rows where a > 4 evaluates to TRUE. X is updated by reference, therefore no assignment needed.
The second expression on the other hand updates a new data.table that's returned by the subset operation. Since the subsetted data.table is ephemeral (it is not assigned to a symbol), the result would be lost; unless the result is assigned, for example, as follows: ans <- DT[a > 4][, b := c].
> sessionInfo()
R version 4.1.3 (2022-03-10)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C
[5] LC_TIME=German_Germany.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] data.table_1.14.2
loaded via a namespace (and not attached):
[1] compiler_4.1.3
The text was updated successfully, but these errors were encountered:
After creation of a data.table
a new one is created the following way:
NB: I did not just assign
b <- a
butb <- a[]
. According to the documentation (see below) I'd expectb
to be independent froma
, and changes not reflect back toa
. On a first glance that seems to be the case:However when using
:=
to assign the new value that logic seems broken:What am I missing from the documentation, which says that the subsetting operation returns a new
data.table
, which then in turn is updated by reference using the:=
operator?The effect is the same when creating
b
asb <- a[TRUE]
. However when usingb <- a[1:.N]
then both objects seem to be separate entities as I'd expect.See Note section of
:=
:The text was updated successfully, but these errors were encountered: