There is a panel dataset. I have to compute a new variable:
cases_new = 3^(lag(cases, n=1L))
for the first date alone. Then the 2nd date onwards, it uses lag (i.e previous instance) on its own value, i.e.:
cases_new = 3^lag(cases_new, n=1L)
I tried to implement using the following code:
df2 <-df1 %>%
mutate(cases_new = if_else(date == "2020-01-01", 3^(lag(cases, n=1L)), 3^(lag(cases_new, n=1L)))
But it threw an error stating that object 'cases_new' not found. I even tried initializing cases_new prior to the above assignment by:
df1$cases_new <- NA
But this did not update cases_new correctly (I get all NAs). Can someone help me out with this particular recursive implementation?