Shift a column of lists in data.table by group in R
In this article, we will discuss how to shift a column of lists in data.table by a group in R Programming Language.
The data table subsetting can be performed and the new column can be created and its values are assigned using the shift method in R. The type can be specified as either “lead” or “lag” depending upon the direction in which the elements are to be moved. The shift method takes as an argument the column name to use the values. The “by” argument in this method illustrates by how many steps to lead or lag the data by.
DT[, col := shift(val, type = ), by = ]
- val – The value to shift
- type – Lead/ lag the values
- by – The positions by which to perform the lag/lead
 "Original Data Table" col1 colid 1: d,e,f 1 2: b,c 1 3: y 2  "Modified Data Table" colid col2 1: 1 b,c 2: 1 NA 3: 2 NA
Explanation: The values belonging to data frames are first grouped into different segments based on values of colid in the data frame. The last instance of every unique value of the colid column is replaced with NA and every other occurrence is replaced by the successor in the same column.
The following code snippet illustrates the code where the elements are subjected to “lag” operation :
 "Original Data Table" col1 colid 1: d,e,f 1 2: b,c 1 3: y 2  "Modified Data Table" colid col2 1: 1 NA 2: 1 d,e,f 3: 2 NA