假设我有以下数据:
SNP eff_allele A1 A2
rs1000000 A A G
rs10000010 C C T
rs1000002 T T C
rs10000023 G T G
我想创建一个新变量alt_allele,它接受列A1或A2的值,具体取决于列eff_allele的值。如果eff_allele等于A1,那么alt_allele应该得到A2的值,如果eff_allele等于A2,那么alt_allele应该得到A1的值。我做了两次尝试:
尝试1:
if (myData$eff_allele == myData$A1) {
myData$alt_allele <- myData$A2
}
if (myData$eff_allele == myData$A2) {
myData$alt_allele <- myData$A1
}
尝试2:
height_fam$alt_allele[height_fam$eff_allele == height_fam$A1] <- height_fam$A2
height_fam$alt_allele[height_fam$eff_allele == height_fam$A2] <- height_fam$A1
这两个都不起作用......我做错了什么?如何对数据进行以下更新:
SNP eff_allele A1 A2 alt_allele
rs1000000 A A G G
rs10000010 C C T T
rs1000002 T T C C
rs10000023 G T G T
答案 0 :(得分:2)
在R
和matlab
中尝试不要使用循环,它们很慢。尝试通过 vector 来解决您的问题。
编辑:哦,我读错了你的问题,你还是没用过载体:)
a=read.table("a.csv", sep = " ", header = T)
row = dim(a)
# Number of rows
row = row[2]
newcol = rep("",row)
A1 = as.character(a$A1)
A2 = as.character(a$A2)
eff_allele = as.character(a$eff_allele)
# a1_ind is FALSE for index that should be equal to A1
a1_ind = eff_allele!= A1
newcol[a1_ind] = A1[a1_ind]
newcol[!a1_ind] = A2[!a1_ind]
a = cbind(a,newcol)
,输出结果为:
SNP eff_allele A1 A2 newcol
1 rs1000000 A A G G
2 rs10000010 C C T T
3 rs1000002 T T C C
4 rs10000023 G T G T