Skip to content

Commit 78b9d85

Browse files
committed
make rcpproll faster by computing all means at once
1 parent e472033 commit 78b9d85

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

backfill_corrections/delphiBackfillCorrection/R/preprocessing.R

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,15 @@ fill_missing_updates <- function(df, value_col, refd_col, lag_col) {
8989
#'
9090
#' @export
9191
get_7dav <- function(pivot_df, refd_col) {
92-
pivot_df <- map_dfc(pivot_df, function(col) {
93-
# If a column contains char or date elements, it is the reference date field;
94-
# skip it. Checking the type here is slightly (10%) faster than passing only
95-
# non-`refd_col` columns into `map_dfc`.
96-
if (inherits(col[1L], "character") || inherits(col[1L], "Date")) {return(col)}
97-
roll_mean(col, 7L, align = "right", fill = NA)
98-
})
92+
pivot_df <- cbind(
93+
# Keep time values at the front
94+
pivot_df[, refd_col],
95+
# Compute moving average of all non-refd columns
96+
RcppRoll::roll_mean(
97+
as.matrix(pivot_df[, names(pivot_df)[names(pivot_df) != refd_col]]),
98+
7L, align = "right", fill = NA
99+
)
100+
)
99101
backfill_df <- pivot_longer(pivot_df,
100102
-refd_col, values_to="value_raw", names_to="issue_date"
101103
)

0 commit comments

Comments
 (0)