Reputation: 321
I'm working with a dataset where it requires me to add prior to prior year data for a column. I want to add the prior to prior year data for not only one column but a list of columns. This is what I've tried,
Company = c("ABC", "ABC", "ABC", "XYZ", "XYZ", "XYZ", "KJF", "KJF", "KJF", "KJF")
Year = c(2021, 2020, 2019, 2017, 2018, 2019, 2020, 2019, 2021, 2018)
REC = c(100, 200, 300, 3000, 4000, 2000, 5000, 6000, 7000, 8000)
S = c(1000, 2000, 3000, 300, 400, 200, 500, 600, 700, 800)
data = data.frame(Company, Year, REC, S, stringsAsFactors = FALSE)
fnames = c("REC", "S")
prevYear = 3
for(i in 1:length(fnames)) {
j = fnames[i]
yrTmp = match(Year - prevYear, Year, nomatch = 0)
data[, sprintf("PP%s", j)] =
data = data %>%
group_by(Company) %>%
mutate(data[, sprintf("PP%s", j)] = data[data[, j], yrTmp]) %>%
ungroup
print(paste("PP", j, "calculated"))
}
I want to add prior to prior year data to REC and S using this loop. In future I want to add more than 40 variables using this loop. How can I achieve it using for loop? This is the desired output,
Upvotes: 0
Views: 406
Reputation: 388817
You can use across
to apply a function to multiple columns.
library(dplyr)
data %>%
group_by(Company) %>%
mutate(across(all_of(fnames), ~.[match(Year - prevYear, Year)],
.names = 'PP_{col}')) %>%
ungroup
Upvotes: 1