I get a data table from a server that shows price predictions depending on the selected month of a calendar year. Basically, data is downloaded from every month of the year. Here is an example data table:
set.seed(123)
dt.data <- data.table(Date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 365),
'BRN Jan-2021' = rnorm(365, 2, 1), 'BRN Jan-2022' = rnorm(365, 2, 1),
'BRN Feb-2021' = rnorm(365, 2, 1), 'BRN Feb-2022' = rnorm(365, 2, 1),
'BRN Mar-2021' = rnorm(365, 2, 1), 'BRN Mar-2022' = rnorm(365, 2, 1),
'BRN Apr-2021' = rnorm(365, 2, 1), 'BRN Apr-2022' = rnorm(365, 2, 1),
'BRN May-2021' = rnorm(365, 2, 1), 'BRN May-2022' = rnorm(365, 2, 1),
'BRN Jun-2021' = rnorm(365, 2, 1), 'BRN Jun-2022' = rnorm(365, 2, 1),
'BRN Jul-2021' = rnorm(365, 2, 1), 'BRN Jul-2022' = rnorm(365, 2, 1),
'BRN Aug-2021' = rnorm(365, 2, 1), 'BRN Aug-2022' = rnorm(365, 2, 1),
'BRN Sep-2021' = rnorm(365, 2, 1), 'BRN Sep-2022' = rnorm(365, 2, 1),
'BRN Oct-2021' = rnorm(365, 2, 1), 'BRN Oct-2022' = rnorm(365, 2, 1),
'BRN Nov-2021' = rnorm(365, 2, 1), 'BRN Nov-2022' = rnorm(365, 2, 1),
'BRN Dec-2021' = rnorm(365, 2, 1), 'BRN Dec-2022' = rnorm(365, 2, 1),
check.names = FALSE)
This data table is quite small as I only created data for the years 2021 and 2022. But there can be several calendar years, or just one calendar year.
Now I would like to calculate daily mean values (based on the date column) for the year 2021 (i.e. the sum of all 12 values per day / date divided by 12 = number of months per calendar year) and save them in a new data table as a column. And now of course the same for 2022.
In this case, the new data table should have the following columns:
| Date | BRN Cal-2021 | BRN Cal-2022 |
where the date column remains unchanged.
The calculation and the column designation for the new data table should always be variable (depending on how many calendar years appear in dt.data
). Basically, it might make sense to organize dt.data
by calendar year at the beginning. But actually I don't really know how to keep the average calculation (daily) variable and general? Or maybe you should create an extra data table for each calendar year, then calculate the mean values and then merge the columns with the daily mean values back into a common data table? However, this should always remain automated (depending on how many calendar years there are). Unfortunately I have no idea how that could be done.
I hope I was able to ask my question accurately enough and someone can help me with my problem.