I have this dataframe:
dat = read.delim(text = "LM, year
85.20000, 2020
56.70000, 2021
49.00000, 2022
71.00000, 2023
33.00000, 2024
96.50000, 2025
26.30000, 2026
21.30000, 2027", sep = ",", header = T)
I want to calculate the percent reduction change from a specific year, I wrote this function regarding that:
pChange = function( dat , startYear){
lastYear = max(dat$year)
res = dat %>% arrange(year) %>%
mutate(change = (LM[year == lastYear] - LM[year == startYear]) / LM[year == startYear])
res
}
pChange( dat, startYear = 2022)
However, I get the same value for every year, and I think there is problem but I don't how to fix it. Any idea?
LM year change
1 85.2 2020 -0.5653061
2 56.7 2021 -0.5653061
3 49.0 2022 -0.5653061
4 71.0 2023 -0.5653061
5 33.0 2024 -0.5653061
6 96.5 2025 -0.5653061
7 26.3 2026 -0.5653061
8 21.3 2027 -0.5653061