I would like to create a table depending on if elements in another table have certain values or characters in R.
Let´s say that I have this table:
test <- data.frame(col1=c(1,2,3,4,5), col2=c("AA","CC","GG","TT", "GG"))
I would like to create a table filing a row with certain values if the character in col2 is AA, other values if is CC, and so on.
I am trying the next script
test2<-c()
for(i in 1:nrow(test)) {
if(test$col2[i]=="AA"){
test2 <- rbind(test2, data.frame(0.91, 0.03, 0.03, 0.03))
} else if (test$col2[i]=="CC"){
test2 <- rbind(test2, data.frame(0.03, 0.91, 0.03, 0.03))
} else if (geno_no_miss$V5[i] == "GG"){
test2 <- rbind(test2, data.frame(0.03, 0.03, 0.91, 0.03))
} else {
test2 <- rbind(test2, data.frame(0.03, 0.03, 0.03, 0.91))
}
}
This is producing the next optput:
X0.91 X0.03 X0.03.1 X0.03.2
1 0.91 0.03 0.03 0.03
2 0.91 0.03 0.03 0.03
3 0.91 0.03 0.03 0.03
4 0.91 0.03 0.03 0.03
5 0.91 0.03 0.03 0.03
My desired output in this example is:
V1 V2 V3 V4
1 0.91 0.03 0.03 0.03
2 0.03 0.91 0.03 0.03
3 0.03 0.03 0.91 0.03
4 0.03 0.03 0.03 0.91
5 0.03 0.03 0.91 0.03
Having the 0.91 in a different position depending on if in my table I have AA or CC or GG or TT. Could you please tell me how I could achieve this?
Aucun commentaire:
Enregistrer un commentaire