r语言 - 比较数据框是否存在不匹配,如果不匹配>则重新分配行



为了替换下面两个数据帧之间的不匹配,我已经创建了一个新的数据帧来替换不匹配。我现在正在寻找一种更有效的方法来使用ifelse或data.table包:

dfA <- structure(list(animal1 = c("AA", "TT", "AG", "CA"), animal2 = c("AA", "TB", "AG", "CA"), animal3 = c("AA", "TT", "AG", "CA")), .Names = c("animal1", "animal2", "animal3"), row.names = c("snp1", "snp2", "snp3", "snp4"), class = "data.frame")
# > dfA
#      animal1 animal2 animal3
# snp1      AA      AA      AA
# snp2      TT      TB      TT
# snp3      AG      AG      AG
# snp4      CA      CA      CA
dfB <- structure(list(animal1 = c("AA", "TT", "AG", "CA"), animal2 = c("AA", "TB", "AG", "DF"), animal3 = c("AA", "TB", "AG", "DF")), .Names = c("animal1", "animal2", "animal3"), row.names = c("snp1", "snp2", "snp3", "snp4"), class = "data.frame")
#> dfB
#     animal1 animal2 animal3
#snp1      AA      AA      AA
#snp2      TT      TB      TB
#snp3      AG      AG      AG
#snp4      CA      DF      DF

当一行中有超过50%的不匹配时,我将"00"分配给snp:的所有列

dfC <- do.call(rbind, lapply(rownames(dfA), function(x){
    mismatchpercentage <- length(which(dfA[x,] != dfB[x,]) == FALSE) / length(dfA[x,]) 
    if(mismatchpercentage > 0.5){
        dfA[x,] <- "00"
    }
    dfA[x, which(dfA[x,] != dfB[x,])] <- "00"
    dfA[x,]
    }))
data.frame(dfC)
# > data.frame(dfC)
#      animal1 animal2 animal3
# snp1      AA      AA      AA
# snp2      TT      TB      00
# snp3      AG      AG      AG
# snp4      00      00      00

这其中的一部分可以通过以下代码完成,但这只是解决方案的一部分,现在我需要用所有00替换最后一行:

as.data.frame(ifelse(as.matrix(dfA) == as.matrix(dfB), as.matrix(dfA), "00"))
#      animal1 animal2 animal3
# snp1      AA      AA      AA
# snp2      TT      TB      00
# snp3      AG      AG      AG
# snp4      CA      00      00

这可以实现你的50%规则:

dfA.m <- as.matrix(dfA)
dfB.m <- as.matrix(dfB)
i.arr <- which(dfA.m != dfB.m, arr.ind=TRUE)
mm <- (dfA.m != dfB.m)  # mismatches
mm[rowSums(mm) > ncol(dfA.m)/2, ] <- TRUE

相关内容

  • 没有找到相关文章

最新更新