从一个大矩阵中,我只想保留包含在额外向量中的某些行。该向量包含矩阵的一些行名。
我使用microbenchmark
比较了四个不同的解决方案,但我想知道R中是否存在更快的解决方案。
以下是可重现的示例:
m <- matrix(rnorm(1e8), nrow = 1e4, ncol = 1e4)
rownames(m) <- paste0("row_", seq(1:nrow(m)))
dim(m); m[1:5, 1:5]
include_list <- paste0("row_", ceiling(runif(1000, 1, 1e4))) # rows to keep
library(microbenchmark)
microbenchmark(
m1 <- m[rownames(m) %in% include_list, , drop = FALSE],
m2 <- m[include_list, , drop = FALSE],
m3 <- m[match(include_list, rownames(m)), , drop = FALSE],
m4 <- subset(m, rownames(m) %in% include_list)
)
以下是microbenchmark
的结果:
Unit: milliseconds
expr min lq mean median uq max neval cld
m3 <- m[match(include_list, rownames(m)), , drop = FALSE] 251.9575 258.0483 279.9949 266.1691 284.8803 422.3918 100 b
m2 <- m[include_list, , drop = FALSE] 251.5875 256.4012 275.9379 263.2740 277.1073 459.7414 100 b
m1 <- m[rownames(m) %in% include_list, , drop = FALSE] 226.1647 229.9530 239.3308 234.0090 239.8762 305.8925 100 a
m4 <- subset(m, rownames(m) %in% include_list) 227.7144 230.8488 242.3036 234.8678 239.1995 388.8809 100 a