I would like to create a column in a data frame based on two other columns. The column names are years and I wanted to provide it as a variables.
For example, if I have a data frame data1 containing several years of data with each column name being the year (I know this might not be a proper name), then:
year = 2022
data1 %>%
select(sprintf("%d", seq(
from = (year - 4),
to = year,
by = 1
))) %>%
mutate(percent = year/(year-1) - 1)
where the (year/(year-1) - 1 would be the percentual change from 2021 to 2022, so the columns 2021 and 2022 would be used. Obviously this code does not work.
Thank you very much for your help.