You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于多匹配条件修改映射表

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:

1. Tidyverse/dplyr Approach (Clean, Readable Syntax)

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 FOXM1 with FOXM1_CANCER_MARKER for probe 202580_x_at
  • Split gene symbols with /// and keep only the first official symbol
  • Add a suffix _X_PROBE to 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 first
  • str_detect() and str_split() handle the multi-gene symbol cleanup cleanly
  • Converting factors to characters prevents errors from locked factor levels
2. Base R Approach (No External Libraries)

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:38:35