I would like the letters "AB" to appear in place of 0, " AA" in the place of 1, and BB in place of -1.
SNP <- data.frame(SNP = c(-1,0,1,-1,1,1,0,-1), SNP1 = c(-1,-1,-1,1,-1,0,1,-1), SNP2 = c(0,0,0,1,-1,-1,-1,1))
CodePudding user response:
dplyr
option:
library(dplyr)
SNP %>%
mutate(across(everything(),
~ case_when(. == -1 ~ "BB",
. == 0 ~ "AB",
. == 1 ~ "AA")))
Output:
SNP SNP1 SNP2
1 BB BB AB
2 AB BB AB
3 AA BB AB
4 BB AA AA
5 AA BB BB
6 AA AB BB
7 AB AA BB
8 BB BB AA
CodePudding user response:
Using match
tmp=c(0,1,-1)
names(tmp)=c("AB","AA","BB")
sapply(SNP,function(x) names(tmp)[match(x,tmp)])
SNP SNP1 SNP2
[1,] "BB" "BB" "AB"
[2,] "AB" "BB" "AB"
[3,] "AA" "BB" "AB"
[4,] "BB" "AA" "AA"
[5,] "AA" "BB" "BB"
[6,] "AA" "AB" "BB"
[7,] "AB" "AA" "BB"
[8,] "BB" "BB" "AA"