-1

I have a DF1

SNP a
rs1 1  
rs2 2  
rs3 3 
rs4 4  

And DF2

SNP  GENE
rs1   A
rs2   B

I am looking for the way to get DF3

SNP    a
rs1_A  1
rs2_B  2
rs3    3
rs4    4

I have tried to use ifelse statement such as

ifelse(DF1$SNP %in% DF2$SNP,paste(DF1$SNP, DF2$GENE,sep="_"),DF1$SNP)

But it didnt work. Thank you for any help.

ThePooh
  • 101
  • 7

1 Answers1

1

We need to replace the %in% with , in paste

DF1$SNP <- ifelse(DF1$SNP %in% DF2$SNP,paste(DF1$SNP, DF2$GENE,sep="_"),DF1$SNP)
DF1$SNP
#[1] "rs1_A" "rs2_B" "rs3"   "rs4"  

If we are creating a new objects

DF3 <- transform(DF1, SNP = ifelse(SNP %in% DF2$SNP, paste(SNP, DF2$GENE, sep="_"), SNP))

NOTE: Here, we assume the columns are character class and not factor

akrun
  • 874,273
  • 37
  • 540
  • 662