lundi 13 novembre 2017

R - assigning value to one column based on a comparison of two other columns

Let's say I have the following data:

SNP eff_allele A1 A2
rs1000000 A A G
rs10000010 C C T
rs1000002 T T C
rs10000023 G T G

I want to create a new variable, alt_allele, that takes on the value of either column A1 or A2, depending on the value of the column eff_allele. If eff_allele equals A1, then alt_allele should get the value of A2, and if eff_allele equals A2, then alt_allele should get the value of A1. I did two attempts, and both don't work...

Attempt 1:

if (myData$eff_allele == myData$A1) {
myData$alt_allele <- myData$A2
}
if (myData$eff_allele == myData$A2) {
myData$alt_allele <- myData$A1
}

Attempt 2:

height_fam$alt_allele[height_fam$eff_allele == height_fam$A1] <- height_fam$A2
height_fam$alt_allele[height_fam$eff_allele == height_fam$A2] <- height_fam$A1

Both of these don't work... What am I doing wrong? How can I achieve the following update to my data:

SNP eff_allele A1 A2 alt_allele
rs1000000 A A G G
rs10000010 C C T T
rs1000002 T T C C
rs10000023 G T G T

Aucun commentaire:

Enregistrer un commentaire