diff --git a/R/R/REDCap_split.r b/R/R/REDCap_split.r index 9f411fb..83d4faf 100644 --- a/R/R/REDCap_split.r +++ b/R/R/REDCap_split.r @@ -112,27 +112,30 @@ REDCap_split <- function(records, metadata) { fields <- rbind(fields, form_complete_fields) # Process ".*\\.factor" fields supplied by REDCap's export data R script - factor_fields <- - do.call( - "rbind", - apply( - fields, - 1, - function(x, y) { - field_indices <- grepl(paste0("^", x[1], "\\.factor$"), y) - if (any(field_indices)) - data.frame( - field_name = y[field_indices], - form_name = x[2], - stringsAsFactors = FALSE, - row.names = NULL - ) - }, - y = names(records) - ) - ) + if (any(grepl("\\.factor$", names(records)))) { - fields <- rbind(fields, factor_fields) + factor_fields <- + do.call( + "rbind", + apply( + fields, + 1, + function(x, y) { + field_indices <- grepl(paste0("^", x[1], "\\.factor$"), y) + if (any(field_indices)) + data.frame( + field_name = y[field_indices], + form_name = x[2], + stringsAsFactors = FALSE, + row.names = NULL + ) + }, + y = names(records) + ) + ) + + fields <- rbind(fields, factor_fields) + } # Identify the subtables in the data subtables <- unique(records$redcap_repeat_instrument)