想要使用现有的3列 - allele_2,allele_1和A1
创建一个新列“non_coded”我想要满足的条件是:
if allele_2 == A1 then non_coded = allele_1
if allele_2 != A1 then non_coded = allele_2
提前致谢,
Rad公司
SNPID chrom STRAND IMPUTED allele_2 allele_1 MAF CALL_RATE HET_RATE
1 rs1000000 12 + Y A G 0.12160 1.00000 0.2146
2 rs10000009 4 + Y G A 0.07888 0.99762 0.1386
HWP RSQ PHYS_POS A1 M1_FRQ M1_INFO M1_BETA M1_SE M1_P
1 1.0000 0.9817 125456933 A 0.1173 0.9452 -0.0113 0.0528 0.83090
2 0.1164 0.8354 71083542 A 0.9048 0.9017 -0.0097 0.0593 0.87000
Hy_MVA$non_coded <- ifelse(Hy_MVA$allele_2 == Hy_MVA$A1, Hy_MVA$allele_1, Hy_MVA$allele_2)
结果:
SNPID chrom STRAND IMPUTED allele_2 allele_1 MAF CALL_RATE HET_RATE
1 rs1000000 12 + Y A G 0.12160 1.00000 0.2146
2 rs10000009 4 + Y G A 0.07888 0.99762 0.1386
HWP RSQ PHYS_POS A1 M1_FRQ M1_INFO M1_BETA M1_SE M1_P non_coded
1 1.0000 0.9817 125456933 A 0.1173 0.9452 -0.0113 0.0528 0.83090 3
2 0.1164 0.8354 71083542 A 0.9048 0.9017 -0.0097 0.0593 0.87000 3
我想要的是什么:
SNPID chrom STRAND IMPUTED allele_2 allele_1 MAF CALL_RATE HET_RATE
1 rs1000000 12 + Y A G 0.12160 1.00000 0.2146
2 rs10000009 4 + Y G A 0.07888 0.99762 0.1386
HWP RSQ PHYS_POS A1 M1_FRQ M1_INFO M1_BETA M1_SE M1_P non_coded
1 1.0000 0.9817 125456933 A 0.1173 0.9452 -0.0113 0.0528 0.83090 G
2 0.1164 0.8354 71083542 A 0.9048 0.9017 -0.0097 0.0593 0.87000 G
答案 0 :(得分:2)
正如Chase所说,使用ifelse()
。我想代码就变成了:
non_coded <- ifelse(allele_2 == A1, allele_1, allele_2)
在看到更新后的问题后,您可以获得数字,因为allele_1
和allele_2
是因素。添加as.character()
应解决此问题:
A1 <- c("A","A","B")
allele_1 <- as.factor(c("A","C","C"))
allele_2 <- as.factor(c("A","B","B"))
non_coded <- ifelse(allele_2 == A1, as.character(allele_1), as.character(allele_2))
non_coded
[1] "A" "B" "C"
答案 1 :(得分:1)
由于您希望non_coded是以下两个值之一:
Hy_MVA$non_coded <- Hy_MVA$allele_2
Hy_MVA$non_coded[Hy_MVA$allele_2 == Hy_MVA$A1] <- Hy_MVA$allele_1[Hy_MVA$allele_2 == Hy_MVA$A1]
仅在allele_2 == A1的行中用allele_1值替换值。听起来好像ifelse将一个因子转换为数字有问题。