Garv
Garv

Reputation: 37

Vectorized Method instead of using for loop

I have a dataframe 'tmp' on which I need to do perform calculation using the last row of another dataframe 'SpreadData'. I am using following code:

for(i in 1:ncol(tmp)){for(j in 1:nrow(tmp)){PNLData[j,i] = 10*tmp[j,i]*SpreadData[nrow(SpreadData),i]}}

Is there any faster method using mapply or something else so that I need not to use for loop.

Thanks

Upvotes: 1

Views: 47

Answers (1)

F. Privé
F. Privé

Reputation: 11728

You can use sweep():

PNLData <- sweep(10 * tmp, 2, SpreadData[nrow(SpreadData), ], "*")

PS1: you can replace SpreadData[nrow(SpreadData), ] by tail(SpreadData, 1).

PS2: I think this makes two copies of your data. If you have a large matrix, you'd better use Rcpp.

Edit: Rcpp solution: put that an .cpp file and source it.

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericMatrix rcppFun(const NumericMatrix& x,
                      const NumericVector& lastCol) {

  int n = x.nrow();
  int m = x.ncol();

  NumericMatrix res(n, m);
  int i, j;

  for (j = 0; j < m; j++) {
    for (i = 0; i < n; i++) {
      res(i, j) = 10 * x(i, j) * lastCol[j];
    }
  }

  return res;
}

And do in R PNLData <- rcppFun(tmp, SpreadData[nrow(SpreadData), ]).

Upvotes: 1

Related Questions