r - 在 R 中计算 big.matrix 的行和?

标签 r bigdata r-bigmemory

我有一个大约有 6000 万行和 150 列的大矩阵(总共大约有 90 亿个元素)。我已将这些数据存储在 big.matrix 对象中(来自包 bigmemory)。现在,我希望计算每行的总和,这是一个问题,因为 big.matrix 是面向列的,所以据我所知,所有汇总函数都是面向列的(例如 >colsumcolmax 等),并且默认情况下没有可用于计算行总和的函数。当然我可以apply(x, 1, sum),但这需要很长时间。我还可以一一循环列并使用矢量化加法来添加它们:

mysum <- rep(0, nrow(x))
for (i in seq(ncol(x))) 
  mysum <- mysum + x[,i]

但这仍然需要 20 多分钟,而且显然不是最理想的,因为它每次循环都会创建一个新的 6000 万元素向量。看来必须有一些更快的方法来做到这一点。

编辑

通过一次处理大约一百万行的 block ,并对这些 block 调用 rowSums,然后连接结果,我将这个时间缩短到了 10 分钟。不过,我仍然有兴趣知道是否有优化的方法来做到这一点。

最佳答案

我已经编写了一些 C++ 代码来执行此操作,改编自 bigmemory Rcpp gallery :

rowSums.cpp

// [[Rcpp::depends(BH)]]
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::depends(BH, bigmemory)]]
#include <bigmemory/MatrixAccessor.hpp>

#include <numeric>

// Logic for BigRowSums.
template <typename T>
NumericVector BigRowSums(XPtr<BigMatrix> pMat, MatrixAccessor<T> mat) {
    NumericVector rowSums(pMat->nrow(), 0.0);
    NumericVector value(1);
    for (int jj = 0; jj < pMat->ncol(); jj++) {
      for (int ii = 0; ii < pMat->nrow(); ii++) {
        value = mat[jj][ii];
        if (all(!is_na(value))) {
          rowSums[ii] += value[0];
        }   
      }   
    }   
    return rowSums;
}

// Dispatch function for BigRowSums
//
// [[Rcpp::export]]
NumericVector BigRowSums(SEXP pBigMat) {
    XPtr<BigMatrix> xpMat(pBigMat);

    switch(xpMat->matrix_type()) {
      case 1:
        return BigRowSums(xpMat, MatrixAccessor<char>(*xpMat));
      case 2:
        return BigRowSums(xpMat, MatrixAccessor<short>(*xpMat));
      case 4:
        return BigRowSums(xpMat, MatrixAccessor<int>(*xpMat));
      case 6:
        return BigRowSums(xpMat, MatrixAccessor<float>(*xpMat));
      case 8:
        return BigRowSums(xpMat, MatrixAccessor<double>(*xpMat));
      default:
        throw Rcpp::exception("unknown type detected for big.matrix object!");
    }   
}

在 R 中:

library(bigmemory)
library(Rcpp)
sourceCpp("rowSums.cpp")

m <- as.big.matrix(matrix(1:9, 3))
BigRowSums(m@address)
[1] 12 15 18

关于r - 在 R 中计算 big.matrix 的行和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24687392/

相关文章:

r - 如何让 R 的 install.packages 在 Dockerfile 中出错?

java - 如何使用Java远程创建Hadoop文件系统的实例?

r - 计算 R 中大矩阵的零空间

r - R 中几个 big.matrix 对象的逐元素平均值

r - R 中 gstat 包中 krige 花费的时间

r - 动态 UI,允许更新过滤器并用 Shiny 的 R 绘制绘图

r - 在 R 中查找两个字符串列之间的匹配项

apache-spark - 如何解决向 Hive 表发送大文件时的连接问题?

hadoop - 如何在 Hadoop 中查找用户运行的作业数量?

c++ - R Rcpp big.matrix加入