I need to make a dataset for a set of years and per year a certain function will calculate the values. I have the program in STATA but am having difficulties to translate it into R. The years are from 2017 to 2025 and a value of 17305004 for the year 2017. From then on the total hours increase with 0.025 per year at a fixed hourprice.
Here is the program in STATA:
set obs 9
scalar growth=.025
scalar hourprice=32
egen year = seq(), from(2017) to(2025)
gen totalhours = .
replace totalhours=17305004 if year==2017
replace totalhours=[(totalhours[_n-1] + growth*totalhours[_n-1])] if year!=2017
format %10.0g totalhours
gen cost = .
replace cost=totalhours*hourprice
format %12.0g cost
list year totalhours cost
year <- 2017:2025
totalhours <- c(17305004, rep(NA, 8))
for(i in 1:8){
totalhours[i+1] <- totalhours[i] + totalhours[i]*.025
}
cost <- totalhours*32
mydata <- data.frame(year, totalhours, cost)
Dataset:
> mydata
year totalhours cost
1 2017 17305004 553760128
2 2018 17737629 567604131
3 2019 18181070 581794234
4 2020 18635597 596339090
5 2021 19101486 611247568
6 2022 19579024 626528757
7 2023 20068499 642191976
8 2024 20570212 658246775
9 2025 21084467 674702944
totalhours
could probably be generated easier, I just couldn't come up with a better way right now.