Repository navigation
x[, .(shift(b)), keyby = a] returns list type (should be int) #5939
Copy link
Copy link
Closed
Description
Activity
This should be included in coming patch release
This should be included in coming patch release
we should absolutely fix this, but it's not going to get us removed from CRAN, so if no one has time for a fix right now, it could be part of 1.15.4 patch release instead
Reacted by Jan GoreckiI 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 NAbut 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.72256881Can you check if #5950 solves your use case?
Can you check if #5950 solves your use case?
Just tried still not working in my case
Apparently, gshift cannot work with subsetting since it always creates vectors of `length(x)` but not length `irows`.Originally posted by @ben-schwen in #5950 (comment)
This becomes clearer when using @renkun-ken example without the assignment by
:=
Upgrading to the latest CRAN version of data.table, expression like
x[, .(shift(b)), keyby = a]will return a list type forV1column, which is unexpected and inconsistent. In previous versions, it always returns<int>.Created on 2024-02-18 with reprex v2.0.2
Session info