'how to change value into character in dataframe
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))
Solution 1:[1]
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"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user2974951 |
