Skip to content

Commit c4c7430

Browse files
committed
continuing to clarify update_predictors
1 parent b976103 commit c4c7430

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

R/data_transforms.R

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,29 @@ get_nonkey_names <- function(epi_data) {
2929
#' modifies the list of preditors so that any which have been modified have the
3030
#' modified versions included, and not the original. Should only be applied
3131
#' after both rolling_mean and rolling_sd.
32-
#' @param epi_data the epi_df
33-
#' @param cols_modified the list of columns to modify. If this is `NULL`, that means we were modifying every column.
34-
#' @param predictors the initial set of predictors; any unmodified are kept, any modified are replaced
32+
#' @param epi_data the epi_df, only included to get the non-key column names
33+
#' @param cols_modified the list of columns which have been modified. If this is `NULL`, that means we were modifying every column.
34+
#' @param predictors the initial set of predictors; any unmodified are kept, any modified are replaced with the modified versions (e.g. "a" becoming "a_m17").
3535
#' @importFrom purrr map map_chr reduce
36+
#' @return returns an updated list of predictors, with modified columns replaced and non-modified columns left intact.
3637
#' @export
3738
update_predictors <- function(epi_data, cols_modified, predictors) {
3839
if (!is.null(cols_modified)) {
3940
# if cols_modified isn't null, make sure we include predictors that weren't modified
40-
other_predictors <- map(cols_modified, ~ !grepl(.x, predictors)) %>% reduce(`&`)
41-
other_predictors <- predictors[other_predictors]
41+
unchanged_predictors <- map(cols_modified, ~ !grepl(.x, predictors, fixed = TRUE)) %>% reduce(`&`)
42+
unchanged_predictors <- predictors[unchanged_predictors]
4243
} else {
43-
other_predictors <- character(0L)
44+
# if it's null, we've modified every predictor
45+
unchanged_predictors <- character(0L)
4446
}
4547
# all the non-key names
4648
col_names <- get_nonkey_names(epi_data)
47-
is_present <- function(x) {
48-
grepl(x, col_names) & !(col_names %in% predictors)
49+
is_present <- function(original_predictor) {
50+
grepl(original_predictor, col_names) & !(col_names %in% predictors)
4951
}
5052
is_modified <- map(predictors, is_present) %>% reduce(`|`)
5153
new_predictors <- col_names[is_modified]
52-
return(c(other_predictors, new_predictors))
54+
return(c(unchanged_predictors, new_predictors))
5355
}
5456

5557
#' get a rolling average for the named columns

0 commit comments

Comments
 (0)