Skip to content
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

documentation of ":=" (or maybe rather "[" actually) #5411

Open
joseffrank opened this issue Jun 27, 2022 · 1 comment
Open

documentation of ":=" (or maybe rather "[" actually) #5411

joseffrank opened this issue Jun 27, 2022 · 1 comment

Comments

@joseffrank
Copy link

After creation of a data.table

library(data.table)
a=fread("
nr,a
1,a
2,b
")
a

a new one is created the following way:

b <- a[]
b

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 new data.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
@jangorecki
Copy link
Member

jangorecki commented Jun 27, 2022

Thank you for reporting. Related issue #3215

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants