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

df2 <- df <- data.frame(a=1) can have different columns after setDT #2170

Open
franknarf1 opened this issue May 17, 2017 · 1 comment
Open

Comments

@franknarf1
Copy link
Contributor

franknarf1 commented May 17, 2017

Cath brought this up in SO chat:

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.

@franknarf1
Copy link
Contributor Author

franknarf1 commented May 17, 2017

@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

Related #1628

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

No branches or pull requests

1 participant