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

Open
#2,170 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by reproducing the examples using setDT, data.frame, data.table, and address(df) versus address(df2). Trace how setDT handles the shared data.frame references, then compare the resulting copy-versus-reference behavior with data.table; done means the intended behavior is established and covered by a regression test.

Written by the indexing model from the issue text.

Description

by-reference

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.

Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.