Skip to content

gshift gives wrong results in subsetting queries #5962

@MichaelChirico

Description

@MichaelChirico
          I encounter this problem too with the following example:
library(data.table)

dt <- expand.grid(
  date = 1:10,
  time = 1:10,
  id = 1:5
)

setDT(dt, key = c("date", "time", "id"))
dt[, x := runif(.N)]
dt[time == 1, x1 := shift(x, type = "lead"), by = id]

previous producing

Key: <date, time, id>
Index: <time>
      date  time    id         x        x1
     <int> <int> <int>     <num>     <num>
  1:     1     1     1 0.1217285 0.2041882
  2:     1     1     2 0.2499479 0.3632787
  3:     1     1     3 0.9992822 0.3553096
  4:     1     1     4 0.9851168 0.1797294
  5:     1     1     5 0.2273191 0.2967562
 ---                                      
496:    10    10     1 0.2503363        NA
497:    10    10     2 0.2226453        NA
498:    10    10     3 0.3320792        NA
499:    10    10     4 0.3255982        NA
500:    10    10     5 0.1976716        NA

but now producing

> dt
Key: <date, time, id>
Index: <time>
      date  time    id          x                                                                    x1
     <int> <int> <int>      <num>                                                                <list>
  1:     1     1     1 0.74905819 0.59769795,0.30605044,0.74046541,0.08694826,0.24060779,0.89059279,...
  2:     1     1     2 0.51506472 0.59769795,0.30605044,0.74046541,0.08694826,0.24060779,0.89059279,...
  3:     1     1     3 0.82395315 0.59769795,0.30605044,0.74046541,0.08694826,0.24060779,0.89059279,...
  4:     1     1     4 0.81986857 0.59769795,0.30605044,0.74046541,0.08694826,0.24060779,0.89059279,...
  5:     1     1     5 0.78196531 0.59769795,0.30605044,0.74046541,0.08694826,0.24060779,0.89059279,...
 ---                                                                                                   
496:    10    10     1 0.53337552                                                                      
497:    10    10     2 0.03719923                                                                      
498:    10    10     3 0.73477434                                                                      
499:    10    10     4 0.03107033                                                                      
500:    10    10     5 0.72256881                               

Originally posted by @renkun-ken in #5939 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    GForceissues relating to optimized grouping calculations (GForce)regression

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions