此代码将核苷酸的向量转换为编号从1到4的编码版本。但是,我想更简洁一些,可能在一行中。这可能吗?
vector2 <- c("c","a","g","g","c","g","g","g","a","t","t","t","c","t","c","t","t","g","t","t","g","a","c","a","g", "a","a","t","c","c")
vector2[vector2=="a"]<-1
vector2[vector2=="c"]<-2
vector2[vector2=="g"]<-3
vector2[vector2=="t"]<-4
as.numeric(vector2)
谢谢
答案 0 :(得分:0)
这是基于R的解决方案:
sapply(vector2, switch, a = 1, c = 2, g = 3, t = 4)
应该有效率更高的东西,但它优雅而行。
答案 1 :(得分:0)
这可能对您有用...
as.numeric(as.factor(c("c","a","g","g","c","g","g","g","a","t","t")))