Reputation: 1209
I have a dataframe (new_df) as below ( sample of original)
structure(list(Facility = c("ABE", "ABE", "ABE", "ABE", "ABE",
"ABE", "ABE", "ABE", "ABE", "ABE"), Year.x = c(2017, 2018, 2019,
2020, 2021, 2022, 2023, 2024, 2025, 2026), `Growth Rate` = c(25.375,
15.5865736124958, 14.5677592486103, 12.3473314371758, 10.253164556962,
6.67251975417032, 2.65906932573599, 1.55411655874191, 1.51211513936965,
1.5853074898301), Year.y = c(2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016), OPS = c(7200, 0, 0, 0, 0, 0, 0, 0, 0,
0)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
I want to input value in OPS such that it shows cumulative times percent increment (column Growth rate) Below is gist of work I am trying to do
For (all facilities in OPSNET) For each facility in OPSNET find the new number of operations for year 2017 to 2045 which is based on the growth rate of each facility for each year. Remember that growth rate is give in “GR”.
Let’s say you want to do that for facility “ABE”. First you multiply 7200 by growth rate in year 2017 (25) and sum it up with 7200 which gives you 9027. Then you multiply 9027 by growth rate in 2018 (15) and sum it up with 9027 which gives you 10434. You do that until year 2045.
I have tried below using dplyr but I am struggling how to multiple with percent increment while taking
my take :-
new_df <- new_df %>% arrange(Facility, Year.x) %>%
mutate(cumsum = cumsum(OPS))
Please assist me!
Upvotes: 0
Views: 423
Reputation: 342
What about this:
for(i in 2:nrow(a)){
if(a$Facility[i]==a$Facility[i-1]){
a$OPS[i] <- (a$OPS[i-1])*((a$`Growth Rate`[i-1]/100)+1)
}
}
Upvotes: 1