Skip to content

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

@jhrmnn

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.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions