I have two data frames A and B, both of the same dimensions. The row and column labels are not guaranteed to be identically ordered between frames.
Both frames contain values 0 and 1, with 1 indicating that a directed “edge” exists between a row and column of the frame (and, accordingly, 0 indicating no connection).
I would like to find “edges” common to both frames. In other words, I want a data frame of the same dimensions as A and B, which contain 1 values where there is a 1 at a row and column of both A and B.
Presently, I am looping through rows and columns and testing if both are 1.
This works, but I imagine there is a more efficient way of doing this. Is there a way to do the equivalent of a “bitwise AND” operation on row vectors of data frames, which returns a row vector I can stuff back into a new data frame? Or is there another more intelligent (and efficient) approach?
EDIT
Matrix multiplication is quite faster than my initial approach. Sorting was the key to making this work.
findCommonEdges <- function(edgesList) {
edgesCount <- length(edgesList)
print("finding common edges...")
for (edgesIdx in 1:edgesCount) {
print(paste("...searching against frame", edgesIdx, sep=" "))
edges <- edgesList[[edgesIdx]]
if (edgesIdx == 1) {
# define commonEdges data frame as copy of first frame
commonEdges <- edges
next
}
#
# we reorder edge data frame row and column labels
# to do matrix multiplication and find common edges
#
edges <- edges[order(rownames(commonEdges)), order(colnames(commonEdges))]
commonEdges <- commonEdges * edges
}
commonEdges
}
You can use normal multiplication for that! 🙂
You could also use logical & operator, which is the “bitwise and” you are looking for. Your expression would then look like
(a & b) + 0(the
+ 0will just convert from boolean back to integer).Note: with dataframes it works exactly the same way.