2017-12-22 19 views
0

"突然変異"と呼ばれる列を持つデータフレームがあります。それらは、 "C> A"のようなSNP、 "+ TTTAAG"のような挿入、または "-CTTGA"のような削除であり得る。例えば: - それぞれ「SNP」、「挿入」または「削除」を書くにデータフレーム内の別の文字ベクトルに基づく条件付き置換

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

私は、Rは、変異列内の特定の文字(「」「>」、「+」または)を検索したいです

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

をしよう:

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

は、私は次のようなものを行うことを試みた:データフレームの新しい列は、私はその結果、以下の期待

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

は、mutation_type列のすべてのセルを上書きします。この問題を解決する方法を教えてください。 grepifelseを使用して

答えて

2

ソリューション:

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 
+1

はあなたに感謝し、完璧にうまく動作します! – makkreker

+0

@makkrekerあなたの問題を解決する助けがあれば、私の答えを受け入れることができます – PoGibas

関連する問題