Simplify rank columns

This commit is contained in:
2024-09-17 09:42:43 -04:00
parent cdeaf6e6ba
commit a58c8db90d

View File

@@ -862,8 +862,8 @@ filter_data <- function(df, variables, nf = FALSE, missing = FALSE, adjust = FAL
if (verbose) message("Replacing NA with 0.001 for Avg_Zscore_ and Z_lm_ columns.") if (verbose) message("Replacing NA with 0.001 for Avg_Zscore_ and Z_lm_ columns.")
df <- df %>% df <- df %>%
mutate( mutate(
across(all_of(avg_zscore_cols), ~ replace_na(., 0.001)), across(all_of(avg_zscore_cols), ~ ifelse(is.na(.), 0.001, .)),
across(all_of(z_lm_cols), ~ replace_na(., 0.001)) across(all_of(z_lm_cols), ~ ifelse(is.na(.), 0.001, .))
) )
} }
@@ -931,24 +931,15 @@ filter_data <- function(df, variables, nf = FALSE, missing = FALSE, adjust = FAL
if (rank) { if (rank) {
if (verbose) message("Calculating rank columns for variables: ", paste(variables, collapse = ", ")) if (verbose) message("Calculating rank columns for variables: ", paste(variables, collapse = ", "))
# Create Rank and Rank_lm columns using mutate and across for (col in avg_zscore_cols) {
df <- df %>% rank_col <- paste0("Rank_", col)
mutate( df[[rank_col]] <- rank(df[[col]], na.last = "keep")
# Rank based on Avg_Zscore_ }
across(all_of(avg_zscore_cols), ~ rank(., na.last = "keep"), .names = "Rank_Avg_Zscore_{.col}"),
# Rank_lm based on Z_lm_
across(all_of(z_lm_cols), ~ rank(., na.last = "keep"), .names = "Rank_lm_Z_lm_{.col}")
)
# Prepare a named vector for renaming columns: new_name = old_name for (col in z_lm_cols) {
rename_vector <- c( rank_lm_col <- paste0("Rank_lm_", col)
setNames(paste0("Rank_", variables), paste0("Rank_Avg_Zscore_", avg_zscore_cols)), df[[rank_lm_col]] <- rank(df[[col]], na.last = "keep")
setNames(paste0("Rank_lm_", variables), paste0("Rank_lm_Z_lm_", z_lm_cols)) }
)
# Rename the rank columns in a single step
df <- df %>%
rename(!!!rename_vector)
} }
return(df) return(df)