Lag creation creates the same non-lagged values

Hi everyone,

I'm having trouble to create a lagged variable using dplyr. I have a panel dataset with 13 variables that I would like to lag.

NUTS2 Year GDP      GDP_lag
1     2000 11689.00 11689.00
2     2000 4797.11  4797.11
3     2000 33533.84 33533.84
4     2000 57659.46 57659.46
5     2000 12378.81 12378.81
1     2001 13589.00 11689.00
2     2001 4959.32  4959.32
3     2001 34117.68 33533.84
4     2001 57659.46 57659.46
5     2001 12378.81 12378.81

My current code is

  group_by(NUTS2_CODE) %>%
  dplyr::mutate(lag1 = dplyr::lag(GDP, n = 1, default = NA)) %>% 
  as.data.frame()
Data_long2_lagged 

The code runs through and I get the same values for the year and NUTS2 region.

Can someone help me and tell me what I am doing wrong?

May thanks!

Thanks for providing code. Could you kindly take further steps to make it easier for other forum users to help you? Share some representative data that will enable your code to run and show the problematic behaviour.

How do I share data for a reprex?

You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

Reprex Guide

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.