For the following dataframe df
:
df <- structure(list(id = c("M0000607", "M0000609", "M0000612"), `2021-08(actual)` = c(12.6,
19.2, 8.3), `2021-09(actual)` = c(10.3, 17.3, 6.4), `2021-10(actual)` = c(8.9,
15.7, 5.3), `2021-11(actual)` = c(7.3, 14.8, 3.1), `2021-12(actual)` = c(6.1,
14.2, 3.5), `2021-08(pred)` = c(11.65443222, 14.31674997, 7.084180415
), `2021-09(pred)` = c(12.29810914, 17.7143733, 6.057927385),
`2021-10(pred)` = c(9.619846116, 15.54553601, 6.525992602
), `2021-11(pred)` = c(8.352097939, 13.97318204, 3.164682627
), `2021-12(pred)` = c(6.113631596, 14.16243166, 3.288372517
), `2021-08(error)` = c(2.082307066, 1.146759554, 0.687406723
), `2021-09(error)` = c(1.631350383, 2.753457736, 2.952737781
), `2021-10(error)` = c(0.945567783, 4.883250027, 1.215819585
), `2021-11(error)` = c(1.998109138, 0.414373304, 0.342072615
), `2021-12(error)` = c(0.719846116, 0.154463985, 1.225992602
)), class = "data.frame", row.names = c(NA, -3L))
Let's say my final objective is to repeatively apply a plotting function to the subsets of df
:
plot_fun <- function(...)
ggplot()
To be more specific, I'll loop each element of year_months <- c('2021-12', '2021-11', '2021-10')
and apply plot_fun
, so firstly, I need to subset selected columns for each element of year_months
.
The logic is: for specific year-month, I will choose: id, actual values of last three months, last one month's error and predicted values. For example, for year_month '2021-12', '2021-11', '2021-10'
, the subset columns are:
2021-12: selected_cols <- c('id', "2021-10(actual)", "2021-11(actual)", "2021-12(actual)",
"2021-12(pred)", "2021-12(error)")
2021-11: selected_cols <- c('id', "2021-09(actual)", "2021-10(actual)", "2021-11(actual)",
"2021-11(pred)", "2021-11(error)")
2021-10: selected_cols <- c('id', "2021-08(actual)", "2021-09(actual)", "2021-10(actual)",
"2021-12(pred)", "2021-12(error)")
Finaly, I'll run code (pseudo) like this:
for year_month in year_months{
selected_cols <- ...
plot_fun(selected_cols)
}
Now my question is how could I write a for loop function to generate column name vector for each of selected year_months?
My trial code (not concise yet) to generate columns for 2021-12
:
date_pre_n_months <- function(end_date, x) {
floor_date(as.Date(end_date) - months(x), 'month')
}
end_date <- '2021-12-31'
begin_date <- date_pre_n_months(end_date, 3)
year_month <- seq(as.Date("2021-10-31"), as.Date("2021-12-31"), by = "month")
act <- paste0(strftime(year_month, format="%Y-%m"), '(actual)')
pred <- paste0(strftime(end_date, format="%Y-%m"), '(pred)')
error <- paste0(strftime(end_date, format="%Y-%m"), '(error)')
selected_cols <- c('id', act, pred, error)
selected_cols
Out:
"id" "2021-10(actual)" "2021-12(actual)" "2021-12(actual)" "2021-12(pred)" "2021-12(error)"