Skip to content

Mixing use of data.frame and data.table in R 3.1+ #710

@arunsrinivasan

Description

@arunsrinivasan

This is something we should basically be aware of. R v3.1.0+ has nice changes and cleverly avoids deep copies wherever possible. And therefore, scenarios like this, however rare, might happen. And we should provide a fix here.

DF1 <- data.frame(x=1:5, y=6:10, z=11:15)
DF2 <- transform(DF1, z = 16:20)

require(data.table)
setorder(setDT(DF2), -z)

DF1
#   x  y  z
#1 5 10 11
#2 4  9 12
#3 3  8 13
#4 2  7 14
#5 1  6 15
DF2
#    x  y  z
#1: 5 10 20
#2: 4  9 19
#3: 3  8 18
#4: 2  7 17
#5: 1  6 16

The fact that x and y are shallow copied results in DF1's columns being affected as well.

On a side note, this also means that if one were to write a function to operate on DF2 using the C API (by reference), then the same thing would happen.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions