 Open in App
Not now

# Shift a column of lists in data.table by group in R

• Last Updated : 24 Oct, 2021

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 = ]`

Arguments :

• val – The value to shift
• type – Lead/ lag the values
• by – The positions by which to perform the lag/lead

## R

 `# importing the required libraries ` `library``(data.table) ` `data_table < - ``data.table``(col1=``c``(``list``(``letters``[4:6]),  ` `                                 ``list``(``letters``[2:3]),  ` `                                 ``list``(``c``(``"y"``))), ` `                          ``colid=``c``(1, 1, 2)) ` ` `  `print``(``"Original Data Table"``) ` `print``(data_table) ` ` `  `print``(``"Modified Data Table"``) ` ` `  `# calling the lead method ` `data_table[, ``.``(col2=``shift``(``.``(col1), type=``"lead"``)[[1L]]), ` `           ``by = colid] `

Output

``` "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 :

## R

 `# importing the required libraries ` `library``(data.table) ` `data_table < - ``data.table``(col1=``c``(``list``(``letters``[4:6]), ` `                                 ``list``(``letters``[2:3]),  ` `                                 ``list``(``c``(``"y"``))), ` `                          ``colid=``c``(1, 1, 2)) ` ` `  `print``(``"Original Data Table"``) ` `print``(data_table) ` ` `  `print``(``"Modified Data Table"``) ` ` `  `# calling the lag method ` `data_table[, ``.``(col2=``shift``(``.``(col1), type=``"lag"``)[[1L]]), ` `           ``by = colid] `

Output

``` "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```

My Personal Notes arrow_drop_up
Related Articles