I want to adjust the prices from different countries to one time zone, which means shifting a column up by lag declared in another column (shift is the time difference).
Using example data like this:
example=data.frame(country=c("IT","IT","GR","GR","GR","TR","TR","TR","TR"),
price=c(200,150,300,480,590,638,237,438,555),
shift=c(0,0,1,1,1,2,2,2,2))
which looks like this:
country price shift
IT 200 0
IT 150 0
GR 300 1
GR 480 1
GR 590 1
TR 638 2
TR 237 2
TR 438 2
TR 555 2
I want to get the following result:
country price shift
IT 200 0
IT 150 0
GR 480 1
GR 590 1
GR NA 1
TR 438 2
TR 555 2
TR NA 2
TR NA 2
I tried to use a solution from this thread: R: Shift values in single column of dataframe UP but since it uses a one value of lag, is not fully applicable.