I am trying to process by group data using dplyr but it is not working. Any help would be appreciated. Below is a sample of the data. I want to retain the value for year 2014 and calculate rest of the values for midfs1 using the lag(midfs1) and value. Below is my attempt at the problem.
t3 = t2 %>%
group_by(cz,btype) %>%
mutate( midfs1 = ifelse(year == 2014,midfs1,
lag(midfs1)*value+lag(midfs1)))
t2 data:
cz btype year midfs value midfs1
1 College 2014 5.4254 0.007582767 5.4254
1 College 2015 5.4779 0.007582767 NA
1 College 2016 5.5191 0.007582767 NA
1 College 2017 5.5616 0.007582767 NA
1 College 2018 5.6097 0.007582767 NA
1 Grocery 2012 4.8267 0.002697526 NA
1 Grocery 2013 4.8205 0.002697526 NA
1 Grocery 2014 4.8583 0.002697526 4.8583
1 Grocery 2015 4.8966 0.002697526 NA
1 Grocery 2016 4.9556 0.002697526 NA
1 Grocery 2017 5.0258 0.002697526 NA
1 Grocery 2018 5.0982 0.002697526 NA
1 Grocery 2019 5.1514 0.002697526 NA
1 Grocery 2020 5.1976 0.002697526 NA
1 Grocery 2021 5.2338 0.002697526 NA
dplyr? If not,data.tablehelps better. However, what is exactly that you want to obtain ("retain the value and calculate the rest" are not too much explicative)?t2 %>% group_by(cz,btype) %>% mutate( midfs1 = ifelse(year == 2014,midfs, lag(midfs)*value+lag(midfs)))