0

I want the previous row value for different groups. I have gone through the solution given here and also tried the code shown below.

new_data[,avg_week := shift(.(avg_travel_time),type = "lag"), by = identifier]

This is the error that I am getting.

Error in `[.data.frame`(new_data, , `:=`(avg_week, c(NA, avg_travel_time[-.N])),  : 
unused argument (by = identifier)
Community
  • 1
  • 1
challa420
  • 260
  • 1
  • 12
  • You have a typo. no need for `.(` inside `shift` `new_data[,avg_week := shift(avg_travel_time, type = "lag"), by = identifier]` – akrun Mar 17 '17 at 06:31
  • Still the same error '> new_data[,avg_week := shift(avg_travel_time, type = "lag"), by = identifier] Error in `[.data.frame`(new_data, , `:=`(avg_week, shift(avg_travel_time, : unused argument (by = identifier)' – challa420 Mar 17 '17 at 06:33

1 Answers1

0

There are two problems in the OP's code, 1) the dataset is a data.frame and not a data.table, 2) use of .( inside shift which is not required. We need to first convert to data.table (setDT(new_data) before applying the data.table syntax

setDT(new_data)[,avg_week := shift(avg_travel_time, type = "lag"), by = identifier]
akrun
  • 874,273
  • 37
  • 540
  • 662