基于多匹配条件修改映射表
Got it, let's work through modifying your probe-to-gene mapping data frame with multiple matching conditions. I’ll share two practical approaches using tools most R users are familiar with:
If you’re comfortable with the tidyverse, dplyr makes multi-condition modifications really intuitive thanks to case_when(). Here’s how to adapt it to your data:
First, let’s start with your original data frame (I’ve converted factors to characters upfront to avoid common factor-level headaches):
library(dplyr) library(stringr) # For string manipulation # Your original data probe_map <- structure( list( REF_ID = structure(1:10, .Label = c("202533_s_at", "202534_x_at", "202551_s_at", "202552_s_at", "202555_s_at", "202565_s_at", "202566_s_at", "202580_x_at", "202581_at", "202589_at"), class = "factor"), GeneSymbol = structure(c(2L, 2L, 1L, 1L, 5L, 6L, 6L, 3L, 4L, 7L), .Label = c("CRIM1 /// LOC101929500", "DHFR", "FOXM1", "HSPA1A /// HSPA1B", "MYLK", "SVIL", "TYMS"), class = "factor") ), .Names = c("REF_ID", "GeneSymbol"), class = "data.frame" ) # Convert factors to characters for easier editing probe_map <- probe_map %>% mutate(across(c(REF_ID, GeneSymbol), as.character))
Now, let’s define and apply your multi-condition rules. For example, let’s say you want to:
- Replace
FOXM1withFOXM1_CANCER_MARKERfor probe202580_x_at - Split gene symbols with
///and keep only the first official symbol - Add a suffix
_X_PROBEto all genes mapped to probes ending in_x_at
Here’s the code:
modified_map <- probe_map %>% mutate( GeneSymbol = case_when( # Priority 1: Exact probe ID match REF_ID == "202580_x_at" ~ "FOXM1_CANCER_MARKER", # Priority 2: Split multi-gene symbols str_detect(GeneSymbol, "///") ~ str_split(GeneSymbol, " /// ", simplify = TRUE)[, 1], # Priority 3: Tag x_at probes str_ends(REF_ID, "_x_at") ~ paste0(GeneSymbol, "_X_PROBE"), # Default: Keep original value if no conditions match TRUE ~ GeneSymbol ) )
Why this works:
case_when()evaluates conditions in order, so higher-priority rules go firststr_detect()andstr_split()handle the multi-gene symbol cleanup cleanly- Converting factors to characters prevents errors from locked factor levels
If you prefer not to load packages, base R can handle this with index-based operations. Here’s the equivalent workflow:
# Start with your original data frame probe_map <- structure( list( REF_ID = structure(1:10, .Label = c("202533_s_at", "202534_x_at", "202551_s_at", "202552_s_at", "202555_s_at", "202565_s_at", "202566_s_at", "202580_x_at", "202581_at", "202589_at"), class = "factor"), GeneSymbol = structure(c(2L, 2L, 1L, 1L, 5L, 6L, 6L, 3L, 4L, 7L), .Label = c("CRIM1 /// LOC101929500", "DHFR", "FOXM1", "HSPA1A /// HSPA1B", "MYLK", "SVIL", "TYMS"), class = "factor") ), .Names = c("REF_ID", "GeneSymbol"), class = "data.frame" ) # Convert factors to characters probe_map$REF_ID <- as.character(probe_map$REF_ID) probe_map$GeneSymbol <- as.character(probe_map$GeneSymbol) # Apply conditions in order of priority # 1. Exact probe ID match probe_map$GeneSymbol[probe_map$REF_ID == "202580_x_at"] <- "FOXM1_CANCER_MARKER" # 2. Clean up multi-gene symbols multi_gene_rows <- grepl("///", probe_map$GeneSymbol) probe_map$GeneSymbol[multi_gene_rows] <- sapply( strsplit(probe_map$GeneSymbol[multi_gene_rows], " /// "), function(x) x[1] ) # 3. Tag x_at probes x_at_rows <- grepl("_x_at$", probe_map$REF_ID) probe_map$GeneSymbol[x_at_rows] <- paste0(probe_map$GeneSymbol[x_at_rows], "_X_PROBE")
Key Tips for Customization:
- Adjust the conditions to match your exact needs (e.g., replace probe IDs, gene symbols, or add new rules)
- Always double-check condition order—earlier rules will override later ones if rows match multiple conditions
- If you need to save the modified data, use
write.csv(modified_map, "updated_probe_map.csv")
内容的提问来源于stack exchange,提问作者J. Smith

