为了替换下面两个数据帧之间的不匹配,我已经设法创建了一个新的数据帧,其中不匹配被替换。我现在正在寻找一种更有效的方法来使用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
答案 0 :(得分:2)
这可以实现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