我有两列
V1 V2
T 1
A 0
C 0
如果列V2是1,那么我想要替换核苷酸互补,如果剩下0就像我写了一个for函数(在我的许多行数据中),但是在他的表现之后我得到了
V1 V2 V3
T 1 NA
A 0 NA
C 1 G
我使用代码
for(i in nrow(Tri1_a)){
if(Tri1_a$V2[i] == 1){
if(Tri1_a$V1[i] == "T")
Tri1_a$V3[i] = "A"
if(Tri1_a$V1[i] == "A")
Tri1_a$V3[i] = "T"
if(Tri1_a$V1[i] == "G")
Tri1_a$V3[i] = "C"
if(Tri1_a$V1[i] == "C")
Tri1_a$V3[i] = "G"
}
else{
Tri1_a$V3[i] = Tri1_a$V1[i]
}
i = i + 1
}
但我想要
V1 V2 V3
T 1 A
A 0 A
C 1 G
哪里出错?
是否可以在没有for的情况下进行,对于使用申请的考试?
答案 0 :(得分:2)
我们可以使用ifelse
:
#dummy data
set.seed(1)
genotype <- data.frame(V1=sample(c("A","C","T","G"),size = 10, replace = T ),
V2=sample(c(0,1), size=10, prob = c(0.6, 0.4), replace=T),
stringsAsFactors = F)
genotype$V3 <- ifelse(genotype$V2 == 0, genotype$V1,
ifelse(genotype$V1=="A", "T",
ifelse(genotype$V1 == "T", "A",
ifelse(genotype$V1=="C", "G","C"))))
genotype
# V1 V2 V3
# 1 C 0 C
# 2 C 0 C
# 3 T 1 A
# 4 G 0 G
# 5 A 1 T
# 6 G 0 G
# 7 G 1 C
# 8 T 1 A
# 9 T 0 T
# 10 A 1 T
答案 1 :(得分:0)
使用命名向量:
#named vector
comp <- c("A", "T", "C", "G")
names(comp) <- c("T", "A", "G", "C")
#map with names when V2 is 1
genotype$V3 <- ifelse(genotype$V2 == 1, comp[genotype$V1], genotype$V1)