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
library(data.table)
df2 <- df <- data.frame(a=1)
setDT(df)[, b := 2]
df2
# the column was not added
# a
# 1: 1
dt2 <- dt <- data.table(a=1)
dt[, b := 2]
dt2
# the column was added
# a b
# 1: 1 2
It looks like the issue is that after setDT, address(df) != address(df2). df2 stayed at the original address while df moved to a new one...? I'm not sure if it's a problem, but it makes the copy-vs-reference thing even harder to explain, I guess.
The text was updated successfully, but these errors were encountered:
@DavidArenburg pointed out that this is part of alloc.col's behavior. Assuming it's deliberate, maybe the setDT docs could use a note about the <- case along the lines of "weird things happen if you have other pointers to the table, so be wary." I think this has come up before in the context of passing DFs to a function that has setDT inside -- it will set the passed DF to DT, but not add columns, etc., based on the code inside the function.
f = function(DF) setDT(DF)[, b := 2]
f(myDF <- data.frame(a = 1))
myDF
# intuitively, one might expect a "b" column here:
# a
# 1: 1
Cath brought this up in SO chat:
It looks like the issue is that after
setDT
,address(df) != address(df2)
.df2
stayed at the original address whiledf
moved to a new one...? I'm not sure if it's a problem, but it makes the copy-vs-reference thing even harder to explain, I guess.The text was updated successfully, but these errors were encountered: