Skip to content

lagging using shift throws error if data.table has only 1 row #1014

@ashinm

Description

@ashinm

Hi I am trying to create a data.table with lagged variables by group id. Certain id's have only 1 row in the data.table in that case the shift operator for lag gives error but the lead operator works fine. Here is an example

dt = data.table(id = 1, week = as.Date('2014-11-11'), sales = 1)
lead = 2
lag = 2
lagSalesNames = paste('lag_sales_', 1:lag, sep = '')
dt[,(lagSalesNames) := shift(sales, 1:lag, NA, 'lag'), by = list(id)]

This gives me the following error

# All items in j=list(...) should be atomic vectors or lists. If you are trying something like 
# `j=list(.SD,newcol=mean(colA))` then use `:=` by group instead (much quicker), or 
# `cbind` or `merge` afterwards.

But if I try the same thing with lead instead, it works fine

dt[,(lagSalesNames) := shift(sales, 1:lag, NA, 'lead'), by = list(id)]

It also seem to work fine if the data.table has more than 1 row e.g. you can try the following with 2 rows which works fine

dt = data.table(id = 1, week = as.Date(c('2014-11-11', '2014-11-11')), sales = 1:2)
dt[,(lagSalesNames) := shift(sales, 1:lag, NA, 'lag'), by = list(id)]

I am using data.table version 1.9.5 on a linux machine with R version 3.1.0. Any help would be much appreciated.

Thanks,
Ashin

Metadata

Metadata

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions