Skip to content

Commit c658c87

Browse files
committed
Improve efficiency of variable matching
1 parent b5d3a77 commit c658c87

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

R/utils.R

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,24 @@ is_verbose_mode <- function() {
1818

1919
# used in both fit.R and csv.R for variable filtering
2020
matching_variables <- function(variable_filters, variables) {
21-
not_found <- c()
22-
selected_variables <- c()
23-
for (v in variable_filters) {
24-
selected <- variables == v | startsWith(variables, paste0(v, "["))
25-
selected_variables <- c(selected_variables, variables[selected])
26-
variables <- variables[!selected]
27-
if (!any(selected)) {
28-
not_found <- c(not_found, v)
29-
}
30-
}
21+
# identify exact matches
22+
matched <- as.list(match(variable_filters, variables))
23+
# loop over filters not exactly matched
24+
for (id in which(is.na(matched))) {
25+
# assign all variable names that match the filter as an array
26+
matched[[id]] <-
27+
which(startsWith(variables, paste0(variable_filters[id], "[")))
28+
}
29+
# collect all selected variables
30+
selected_variables <- variables[unlist(matched)]
31+
# collect all filters not found
32+
not_found <- variable_filters[vapply(matched, length, 0L) == 0]
3133
list(
3234
matching = selected_variables,
3335
not_found = not_found
3436
)
3537
}
3638

37-
3839
# checks for OS and hardware ----------------------------------------------
3940

4041
os_is_windows <- function() {

0 commit comments

Comments
 (0)