R 中数据帧行上的按位 AND 或类似运算?
我有两个数据框 A
和 B
,它们的尺寸相同。不保证帧之间的行标签和列标签的顺序相同。
两个帧都包含值 0
和 1
,其中 1
表示帧的行和列之间存在有向“边缘”(并且,相应地,0
表示没有连接)。
我想找到两个框架共有的“边缘”。换句话说,我想要一个与 A
和 B
尺寸相同的数据框,其中包含 1
值,其中有一个 1
位于A
和B
的行和列处。
目前,我正在循环行和列并测试两者是否都是1
。
这是可行的,但我想有一种更有效的方法可以做到这一点。有没有一种方法可以对数据帧的行向量执行相当于“按位与”操作,该操作返回一个行向量,我可以将其塞回到新的数据帧中?或者还有另一种更智能(且高效)的方法吗?
编辑
矩阵乘法比我最初的方法要快得多。排序是完成这项工作的关键。
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
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用普通乘法来实现! :-)
您还可以使用逻辑 &运算符,这是您正在寻找的“按位与”。您的表达式将类似于
(a & b) + 0
(
+ 0
只会从布尔值转换回整数)。注意:对于数据框,它的工作方式完全相同。
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
+ 0
will just convert from boolean back to integer).Note: with dataframes it works exactly the same way.
也许是这样的?
我最终得到了一个矩阵,但如果需要,您可以将其再次转换回数据框。但如果您只处理 0/1 数据,则没有真正的理由不使用矩阵。 (话又说回来,我不太了解你的具体情况……)
Something like this maybe?
I've ended up with a matrix, but you can convert it back to a data frame again, if need be. But if you're just dealing with 0/1 data, there's no real reason not to use matrices. (Then again, I don't know many details about your specific situation...)