I think I have been unconsciously roaming around this question for some time.
Let me try to explain it clearly. Suppose I have a datatable such as this one :
library(tidyverse)
library(data.table)
library(lubridate)
MWE <- as.data.table(ggplot2::economics) %>%
.[,c("pce","psavert","uempmed","unemploy"):=NULL]
> MWE
date pop
1: 1967-07-01 198712.0
2: 1967-08-01 198911.0
3: 1967-09-01 199113.0
4: 1967-10-01 199311.0
5: 1967-11-01 199498.0
---
570: 2014-12-01 319746.2
571: 2015-01-01 319928.6
572: 2015-02-01 320074.5
573: 2015-03-01 320230.8
574: 2015-04-01 320402.3
There are several things I want to do that imply creating new variables that for a give row i
depends on other variables at this row.
For instance, if I want the evolution of population for one month to another, what I want is something like :
MWE2 <- MWE[, MoM :=pop[date=i$date]/pop[date=(i$date-month(1))]
The same if I want a Year on year evolution :
MWE3 <- MWE[, YoY :=pop[date=i$date]/pop[date=(i$date-year(1))]
And if for instance I want to create the difference between the current value and the average of the population for the same month for the 5 previous years, it would be someting like
MWE4 <- MWE[, 5YD :=pop- mean(pop[month(date)=month(i$date) & (year(date$i)-6 <= year(date) < year(date$i))])]
So is there a way to do something close to what I envision ? Do not hesitate to tell me if I have been unclear