setDT modifies the class of the origin data.frame

Open
#4,784 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start at the setDT entry point and reproduce the issue with the data.frame example from the report. Inspect the two places where setattr and setalloccol are called, then verify that converting d2 leaves d1 with class data.frame while d2 becomes data.table.

Written by the indexing model from the issue text.

Description

by-reference

Take this code:

> d1 <- data.frame(a=c(1,2,3,4), b=c(2,3,4,5))
> d2 <- d1
> setDT(d2)  

At this point d2 is a shallow copy of d1, meaning they are not the same R object (although they point to the same columns).
d2's class is data.table, as expected, but so is d1

> class(d2)
[1] "data.table" "data.frame"
> class(d1)
[1] "data.table" "data.frame"

Unlike #4783 , I think this is an easy fix. In two places in setDT's source you see the pair of lines -

setattr(x, "class", .resetclass(x, "data.frame"))
setalloccol(x)

setalloccol is what initiates the shallow copy. If the order of the rows were reversed, the class of only the current shallow copy (d2 in the example above) would be modified, as expected.

# Output of sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)
Matrix products: default

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.