簡體   English   中英

基於數據幀中另一個字符向量的向量中的條件替換

[英]Conditional replacement in vector based on the another character vector in dataframe

我有一個稱為“突變”的數據框。 它們可以是SNP(如“ C> A”),插入(如“ + TTTAAG”)或刪除(如“ -CTTGA”)。 例如:

**position** **mutation**
1234           C > A
1452           +TTTAAG
2734           -CTTGA

我想讓R在突變列(“>,” +“或”-“)中搜索特定字符,並將” SNP“,”插入“或”刪除“分別寫入數據幀的新列中,所以我期望結果:

**position** **mutation**  **mutation_type**
1234           C > A             SNP
1452           +TTTAAG         insertion
2734           -CTTGA           deletion

我試圖做以下事情:

mutation_type <- rep(NA, length(df$position)))
df$mutation_type <- mutation_type #creating a new column with NAs

試:

while(grep(pattern = "-", df$mutation)){
  df$mutation_type <- "deletion"
}

只需覆蓋mutation_type列中的每個單元格即可。 請給我一個建議,如何解決這個問題?

使用grepifelse解決方案:

genotype <- data.frame(position = 1:3,
                       mutation = c("C > A", "+TGCA", "-ACGT"))
genotype$mutation_type <- 
    ifelse(grepl("\\+", genotype$mutation), "Insertion", 
           ifelse(grepl("\\-", genotype$mutation), "Deletion", "SNP"))

  position mutation mutation_type
1        1    C > A           SNP
2        2    +TGCA     Insertion
3        3    -ACGT      Deletion

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM