Skip to content

Weird behaviour using .SD, by= and copy #958

Closed

Description

@jhrmnn

I've stumbled upon a really weird behaviour with 1.9.4. The minimal example is

library(data.table)
dt <- data.table(a=c(1, 1, 1, 0, 0),
                 b=c("A", "B", "A1", "A", "B"))
dt
##    a  b
## 1: 1  A
## 2: 1  B
## 3: 1 A1
## 4: 0  A
## 5: 0  B
dt[, nrow(.SD[b == 'B']), by=.(a)]
##    a V1
## 1: 1  1
## 2: 0  0
dt[, nrow(copy(.SD)[b == 'B']), by=.(a)]
##    a V1
## 1: 1  1
## 2: 0  1
dt[3, b:="C"]
##    a b
## 1: 1 A
## 2: 1 B
## 3: 1 C
## 4: 0 A
## 5: 0 B
dt[, nrow(.SD[b == 'B']), by=.(a)]
##    a V1
## 1: 1  1
## 2: 0  1
dt[, nrow(copy(.SD)[b == 'B']), by=.(a)]
##    a V1
## 1: 1  1
## 2: 0  1

Note that when dt[3, b] == "A1", the output with and without copy differs, which I don't understand at all. When dt[3, b] == "C", the behaviour is as expected.

Activity

  1. arunsrinivasan commented on Nov 17, 2014

    @arunsrinivasan
    Member

    Thanks, I've reproduced the bug on 1.9.5 as well.

    For the future, please read and follow the instructions here and here.

  2. added this to the v1.9.6 milestone on Nov 17, 2014
  3. arunsrinivasan commented on Nov 17, 2014

    @arunsrinivasan
    Member

    The issue is due to automatic indexing creating secondary key attribute on .SD.

    require(data.table)
    options(datatable.auto.index = FALSE)
    dt <- data.table(a=c(1, 1, 1, 0, 0),
                     b=c("A", "B", "A1", "A", "B"))
    dt[, nrow(.SD[b == 'B']), by=.(a)]
    #    a V1
    # 1: 1  1
    # 2: 0  1
  4. jangorecki commented on Dec 17, 2014

    @jangorecki
    Member

    the issue was solved by following line:
    is.null(attr(x, '.data.table.locked'))) { # fix for #958, don't create auto index on '.SD'.
    isn't it unsafe to use attr(..., exact=FALSE)?
    I know it is unlikely but such error caused by some approximation of names can be hard to debug if the errors occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions