Skip to content

Multiple column assignment using :=(...) with shift() creates list column types in 1.14.3 #5403

@ben519

Description

@ben519

Minimal Reprex

The following code works as expected

dt <- data.table(grp = c(1,1,1,2,2,2), x = 1:6)
dt[, x_prev := shift(x = x, type = "lag", n = 1), by = grp]
dt[, x_next := shift(x = x, type = "lead", n = 1), by = grp]

print(dt)
     grp     x x_prev x_next
   <num> <int>  <int>  <int>
1:     1     1     NA      2
2:     1     2      1      3
3:     1     3      2     NA
4:     2     4     NA      5
5:     2     5      4      6
6:     2     6      5     NA

but when I try to create x_prev and x_next in one fell swoop, the resulting fields are lists of vectors

dt <- data.table(grp = c(1,1,1,2,2,2), x = 1:6)
dt[, `:=`(
  x_prev = shift(x = x, type = "lag", n = 1),
  x_next = shift(x = x, type = "lead", n = 1)
), by = grp]

print(dt)
     grp     x            x_prev            x_next
   <num> <int>            <list>            <list>
1:     1     1 NA, 1, 2,NA, 4, 5  2, 3,NA, 5, 6,NA
2:     1     2 NA, 1, 2,NA, 4, 5  2, 3,NA, 5, 6,NA
3:     1     3 NA, 1, 2,NA, 4, 5  2, 3,NA, 5, 6,NA
4:     2     4 NA, 1, 2,NA, 4, 5  2, 3,NA, 5, 6,NA
5:     2     5 NA, 1, 2,NA, 4, 5  2, 3,NA, 5, 6,NA
6:     2     6 NA, 1, 2,NA, 4, 5  2, 3,NA, 5, 6,NA

The same code works properly on data.table 1.14.2, so this appears to be a bug introduced by 1.14.3.


> sessionInfo()
R version 4.2.0 (2022-04-22)
Platform: x86_64-apple-darwin21.3.0 (64-bit)
Running under: macOS Monterey 12.4

Matrix products: default
LAPACK: /usr/local/Cellar/r/4.2.0/lib/R/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.14.3

loaded via a namespace (and not attached):
[1] compiler_4.2.0  httr_1.4.3      R6_2.5.1        generics_0.1.2  tools_4.2.0     lubridate_1.8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    GForceissues relating to optimized grouping calculations (GForce)bugdev

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions