setDT modifies the class of the origin data.frame

Aperta
#4,784 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
r
Ambito
data

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
R
Stelle
3.9k
Fork
1.1k
Merge medio
14h 4m
PR unite (30g)
4

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Rdatatable/data.table

Tutte le issue di Rdatatable/data.table

Issue simili

Altre issue su R

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.