Browse Source

Avoid get() in across_all

Bryan Roessler 7 months ago
parent
commit
4877c1413e
1 changed files with 23 additions and 10 deletions
  1. 23 10
      qhtcp-workflow/apps/r/calculate_interaction_zscores.R

+ 23 - 10
qhtcp-workflow/apps/r/calculate_interaction_zscores.R

@@ -602,16 +602,29 @@ generate_box_plot <- function(plot, config) {
 
 
 # Adjust missing values and calculate ranks
 # Adjust missing values and calculate ranks
 adjust_missing_and_rank <- function(df, variables) {
 adjust_missing_and_rank <- function(df, variables) {
-
-  # Adjust missing values in Avg_Zscore and Z_lm columns, and apply rank to the specified variables
-  df <- df %>%
-    mutate(across(all_of(variables), list(
-      Avg_Zscore = ~ if_else(is.na(get(paste0("Avg_Zscore_", cur_column()))), 0.001, get(paste0("Avg_Zscore_", cur_column()))),
-      Z_lm = ~ if_else(is.na(get(paste0("Z_lm_", cur_column()))), 0.001, get(paste0("Z_lm_", cur_column()))),
-      Rank = ~ rank(get(paste0("Avg_Zscore_", cur_column()))),
-      Rank_lm = ~ rank(get(paste0("Z_lm_", cur_column())))
-    ), .names = "{fn}_{col}"))
-
+  
+  # Loop over each variable
+  for (var in variables) {
+    # Construct column names
+    avg_zscore_col <- paste0("Avg_Zscore_", var)
+    z_lm_col <- paste0("Z_lm_", var)
+    rank_col <- paste0("Rank_", var)
+    rank_lm_col <- paste0("Rank_lm_", var)
+    
+    # Check if the columns exist in the data frame
+    if (all(c(avg_zscore_col, z_lm_col) %in% names(df))) {
+      # Adjust missing values by replacing NA with 0.001
+      df[[avg_zscore_col]] <- if_else(is.na(df[[avg_zscore_col]]), 0.001, df[[avg_zscore_col]])
+      df[[z_lm_col]] <- if_else(is.na(df[[z_lm_col]]), 0.001, df[[z_lm_col]])
+      
+      # Compute ranks and create new columns
+      df[[rank_col]] <- rank(df[[avg_zscore_col]], na.last = "keep")
+      df[[rank_lm_col]] <- rank(df[[z_lm_col]], na.last = "keep")
+    } else {
+      warning(paste("Columns", avg_zscore_col, "or", z_lm_col, "not found in data frame"))
+    }
+  }
+  
   return(df)
   return(df)
 }
 }