在R中无需创建临时副本对选中行列执行布尔AND/OR操作
Great question—when dealing with massive boolean matrices in R, avoiding unnecessary temporary copies is key to keeping memory usage low and operations fast. The apply() approach you're using does indeed create a copy of the subset matrix, which is inefficient for large datasets. Here are a few better alternatives:
1. Base R: Use Reduce() with Column-wise Extraction
Instead of creating a full subset of your columns, you can process each column individually and accumulate the result with Reduce(). This avoids generating a large temporary matrix, since we only pull one column (a vector) at a time:
For "All TRUE" (Row-wise AND)
cols <- c(1,5,6) all_true <- Reduce(`&`, lapply(cols, function(c) toymat[, c]))
For "Any TRUE" (Row-wise OR)
any_true <- Reduce(`|`, lapply(cols, function(c) toymat[, c]))
Testing this on your toy matrix:
toymat <- matrix(sample(c(F,T),50,rep=T),5,10) cols <- c(1,5,6) Reduce(`&`, lapply(cols, function(c) toymat[, c])) # Output: [1] FALSE FALSE FALSE FALSE FALSE
This matches the result from your apply() call, but without creating a copy of the column subset.
2. Use the matrixStats Package (Optimized for Matrices)
The matrixStats package has purpose-built functions for row-wise operations that work directly on the original matrix without creating subsets. These are highly optimized and perfect for large datasets:
First install and load the package if you haven't already:
install.packages("matrixStats") library(matrixStats)
For "All TRUE"
all_true <- rowAlls(toymat, cols = cols)
For "Any TRUE"
any_true <- rowAnys(toymat, cols = cols)
The rowAlls() and rowAnys() functions directly reference the specified columns in the original matrix, so no temporary copies are made. This is the most efficient option for very large matrices.
Why apply() Isn't Ideal Here
When you run apply(toymat[, cols], 1, all), R first creates a copy of toymat[, cols] (the subset matrix), then converts each row of that subset into a vector for the all() function. For a massive matrix, this copy can consume a huge amount of memory and slow down your code significantly.
内容的提问来源于stack exchange,提问作者bluemouse

