I have a dataframe with a value column and the corresponding year. I want to create an additional column that should contain the ratio of value for years at an interval of 5 years going backward. EX. If the year is 2000, the 'newval' column should have the ratio for value for the year 2000 and 1995. My data frame looks like. There might be a missing year and no data in both the value and year column.
df2 = data.frame(code = c("AFG", "AGO", "ALB", "AND", "ARB", "ARE", "ARG", "ARM", "ASM", "ATG", "AUS", "AUT","AUT"),
val = c(123, 42, 23, 5, 42, 4, 23, 25, 42, 23, NA, 5563,56),
Year = c(1990, 1991, 1992, 1993, 1991, 1995, 1996, 1997, 1991, 1992, 2000, 2001,2002))
The final dataset should look like this
df2 = data.frame(code = c("AFG", "AGO", "ALB", "AND", "ARB", "ARE", "ARG", "ARM", "ASM", "ATG", "AUS", "AUT","AUT"),
val= c(123, 42, 23, 5, 42, 4, 23, 25, 42, 23, NA, 5563,56),
Year = c(1990, 1991, 1992, 1993, 1991, 1995, 1996, 1997, 1991, 1992, 2000, 2001,2002), newval=c(NA,NA,NA,NA,NA,0.032520325,0.547619048,1.086956522,NA,NA,NA,241.8695652,2.24))
r
– Sotos