Skip to contents

Calculate lagged values by column

Usage

change_lagged(df, col, by = NULL)

Arguments

df

data frame

col

column of numeric values to calculate lag

by

column to calculate lag by

Examples

mock_provider(2020:2025) |>
  dplyr::group_by(group) |>
  change_lagged(payment, year)
#> # A tibble: 12 × 5
#> # Groups:   group [2]
#>     year group payment payment_chg payment_pct
#>    <int> <chr>   <int>       <int>       <dbl>
#>  1  2020 A       12614          NA    NA      
#>  2  2021 A       13371         757     0.0600 
#>  3  2022 A       11980       -1391    -0.104  
#>  4  2023 A       12046          66     0.00551
#>  5  2024 A       13331        1285     0.107  
#>  6  2025 A       18943        5612     0.421  
#>  7  2020 B       19285          NA    NA      
#>  8  2021 B       16293       -2992    -0.155  
#>  9  2022 B       11022       -5271    -0.324  
#> 10  2023 B       11289         267     0.0242 
#> 11  2024 B       19121        7832     0.694  
#> 12  2025 B       14275       -4846    -0.253