A nested ifelse
should do it (take a look at help(ifelse)
for usage):
ifelse(dat$Genotype==dat$S288C,1,ifelse(dat$Genotype==dat$SK1,0,NA))
With this test data:
> dat
Genotype S288C SK1
[1,] "G" "A" "G"
[2,] "G" "A" "G"
[3,] "C" "T" "C"
[4,] "G" "A" "G"
[5,] "G" "G" "T"
[6,] "G" "A" "A"
We get:
> ifelse(dat$Genotype==dat$S288C,1,ifelse(dat$Genotype==dat$SK1,0,NA))
[1] 0 0 0 0 1 NA
(Note: If you have trouble using this, you'll want to make sure that the columns are vectors, and are not treated by R as factors...a simple for loop should do it: for (i in 1:ncol(dat)){dat[,i]=as.vector(dat[,i])}
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…