Here is a toy dataframe, mydf
M1 M2 M3 M4 M5 M6 M7 M8
A1 2 2 1 1 1 1 0 2
A2 0 1 2 2 1 1 0 2
A3 2 0 0 1 1 0 1 1
A4 2 0 1 2 2 1 1 0
A5 1 2 0 2 2 1 0 1
A6 2 1 1 2 2 1 1 1
And here is a screenshot of second dataframe, mismatches:
M1 M2 M3 M4 M5 M6 M7 M8
A1-A2 TRUE NA NA NA NA NA FALSE FALSE
A1-A3 FALSE TRUE NA NA NA NA NA NA
A1-A4 FALSE TRUE NA NA NA NA NA TRUE
A1-A5 NA FALSE NA NA NA NA FALSE NA
A1-A6 FALSE NA NA NA NA NA NA NA
A2-A1 TRUE NA NA NA NA NA FALSE FALSE
A2-A3 TRUE NA TRUE NA NA NA NA NA
A2-A4 TRUE NA NA FALSE NA NA NA TRUE
A2-A5 NA NA TRUE FALSE NA NA FALSE NA
A2-A6 TRUE NA NA FALSE NA NA NA NA
Now I'd like to define a function to compare the two dataframes where mismatches[a,] == TRUE & mydf[b,] != 1). So for the trio A1-A2/A3, the value for column M1 should be TRUE since mismatches for A1-A2 == TRUE & mydf for A3 !=1. Here is a snapshot of my desired result
M1 M2 M3 M4 M5 M6 M7 M8
A1-A2/A3 TRUE NA NA NA NA NA FALSE FALSE
A1-A2/A4 TRUE NA NA NA NA NA FALSE FALSE
A1-A2/A5 FALSE NA NA NA NA NA FALSE FALSE
A1-A2/A6 TRUE NA NA NA NA NA FALSE FALSE
A1-A3/A2 FALSE FALSE NA NA NA NA NA NA
A1-A3/A4 FALSE TRUE NA NA NA NA NA NA
A1-A3/A5 FALSE TRUE NA NA NA NA NA NA
A1-A3/A6 FALSE FALSE NA NA NA NA NA NA
I can't seem to find the question that I used as a model for my working code but here are some similar postsCount Number of Pairwise Differences...;Compare all the columns pairwise...;Pairwise ... ignoring empty values. Mismatches was derived from mydf by identifying 0/2 and 2/0 pairwise comparisons.
#make a 2/0 matrix for for calculating incompatibilities
nohets <- mydf
nohets[nohets == 1] <- NA
#the incompatibility function
incompatible <- function(a,b,x)
x[a,]!=x[b,]
The idea was to use this same strategy to compare mydf and mismatches.
#make a trio function
trio_id <- function(a,b,x,y)
x[a,] == TRUE & y[b,] !=1
rows <- c(row.names(mismatches),row.names(mydf))
grid <- subset(expand.grid(Var1=rows,Var2=rows))
impossible <- t(mapply(trio_id,grid$Var2,grid$Var1,
MoreArgs=list(x=mismatches,y=mydf)))
dimnames(impossible) <- list(paste(grid$Var2,grid$Var1,
sep="/"),names(mydf))
This runs, but does not give the desired result but rather
M1 M2 M3 M4 M5 M6 M7 M8
A1-A2/A3 NA NA NA NA NA NA FALSE FALSE
A1-A2/A4 NA NA NA NA NA NA FALSE FALSE
A1-A2/A5 NA NA NA NA NA NA FALSE FALSE
A1-A2/A6 NA NA NA NA NA NA FALSE FALSE
A1-A3/A2 FALSE NA NA NA NA NA NA NA
A1-A3/A4 FALSE NA NA NA NA NA NA NA
A1-A3/A5 FALSE NA NA NA NA NA NA NA
A1-A3/A6 FALSE NA NA NA NA NA NA NA
I don't need every comparison calculated... and I suspect writing the code for every possible comparison in 'grid' is the source of my problem.