我有一个包含任何这些值的数据框。
from=c("A","C","G","T","R","Y","M","K","W", "S","N")
我想相应地替换为
to=c("AA","CC","GG","TT","AG","CT","AC","GT","AT", "CG","NN")
最好的方法是什么,循环所有要替换的值?或循环矩阵位置。或任何其他解决方案?
dd<-matrix(sample(from, 100, replace=TRUE), 10)
dd
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "K" "S" "G" "T" "R" "N" "A" "C" "W" "M"
[2,] "Y" "K" "S" "G" "T" "R" "N" "A" "C" "W"
[3,] "M" "Y" "K" "S" "G" "T" "R" "N" "A" "C"
[4,] "W" "M" "Y" "K" "S" "G" "T" "R" "N" "A"
[5,] "C" "W" "M" "Y" "K" "S" "G" "T" "R" "N"
[6,] "A" "C" "W" "M" "Y" "K" "S" "G" "T" "R"
[7,] "N" "A" "C" "W" "M" "Y" "K" "S" "G" "T"
[8,] "R" "N" "A" "C" "W" "M" "Y" "K" "S" "G"
[9,] "T" "R" "N" "A" "C" "W" "M" "Y" "K" "S"
[10,] "G" "T" "R" "N" "A" "C" "W" "M" "Y" "K"
我使用了从到到的循环。
myfunc<-function(xx){
from=c("A","C","G","T","R","Y","M","K","W", "S","N");
to=c("AA","CC","GG","TT","AG","CT","AC","GT","AT", "CG","NN");
for (i in 1:11){
xx[xx==from[i]]<-to[i];
}
return(xx);
}
它适用于小矩阵,但对于大矩阵需要很长时间。有什么有效的解决办法吗?
谢谢