积累先验结果的更有效的R函数

积累先验结果的更有效的R函数,r,R,我有两个数据帧,如下所示: totPrimas: 54 54 54 ... 54 56 55 ... 54 56 55 ... ... and a: 0.998 0.988 0.958 ... 0.997 0.978 0.958 ... 0.995 0.878 0.948 ... ... 我想乘以totPrimas的第一行*a的第一行。totPrimas[1,]*a[1,]加上totPrimas[2]的结果,我想乘以a[2],以此类推 我写了一个函数,但是速度太慢了,实际数据帧是5642

我有两个数据帧,如下所示:

totPrimas:

54 54 54 ...
54 56 55 ...
54 56 55 ...
...

and a:

0.998 0.988 0.958 ...
0.997 0.978 0.958 ...
0.995 0.878 0.948 ...
...
我想乘以totPrimas的第一行*a的第一行。totPrimas[1,]*a[1,]加上totPrimas[2]的结果,我想乘以a[2],以此类推

我写了一个函数,但是速度太慢了,实际数据帧是5642000

b<- matrix(0, nrow = 10, ncol = 10)
b<- as.data.frame(b)


        prova3 <- function(i){

        if(i==1){
        b[1,] <<- totPrimas[i,]*a[i,]

        }else{

        b[i,] <<- (b[i-1,] + totPrimas[i,])*a[i,]
        }

        }

    sapply(1:10, prova3)
b
让我们简单地翻译一下Rcpp:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector fun(const NumericVector x, const NumericVector y) {
  NumericVector z(x.size());
  z(0) = x(0) * y(0);
   for(int i = 1; i < x.size(); i++) {
     z(i) = (z(i-1) + x(i)) * y(i);
   }
   return z;
}
读者练习:

  • 在Rcpp中的列上循环
  • 使用递归
  • 让我们简单地翻译一下Rcpp:

    #include <Rcpp.h>
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    NumericVector fun(const NumericVector x, const NumericVector y) {
      NumericVector z(x.size());
      z(0) = x(0) * y(0);
       for(int i = 1; i < x.size(); i++) {
         z(i) = (z(i-1) + x(i)) * y(i);
       }
       return z;
    }
    
    读者练习:

  • 在Rcpp中的列上循环
  • 使用递归
  • 让我们简单地翻译一下Rcpp:

    #include <Rcpp.h>
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    NumericVector fun(const NumericVector x, const NumericVector y) {
      NumericVector z(x.size());
      z(0) = x(0) * y(0);
       for(int i = 1; i < x.size(); i++) {
         z(i) = (z(i-1) + x(i)) * y(i);
       }
       return z;
    }
    
    读者练习:

  • 在Rcpp中的列上循环
  • 使用递归
  • 让我们简单地翻译一下Rcpp:

    #include <Rcpp.h>
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    NumericVector fun(const NumericVector x, const NumericVector y) {
      NumericVector z(x.size());
      z(0) = x(0) * y(0);
       for(int i = 1; i < x.size(); i++) {
         z(i) = (z(i-1) + x(i)) * y(i);
       }
       return z;
    }
    
    读者练习:

  • 在Rcpp中的列上循环
  • 使用递归

  • 只是想确保我理解:结果的
    j
    第行条目将是
    (totPrimas[1,]*cumprod(a[1:j,])+(totPrimas[2,]*cumprod(a[2:j,])+…+(totPrimas[j,]*a[j,])
    ?这看起来对吗?嗨,格雷戈。它将是:(totPrimas[1,]*prod(a[1:j,])+(totPrimas[2,]*prod(a[2:j,])+…+(totPrimas[j,]*a[j,])。谢谢,我只是想确保我理解:结果的
    j
    第行条目将是
    (totPrimas[1,]*cumprod(a[1:j,])+(totPrimas[2,]*cumprod(a[2:j,])+…+(totPrimas[j,]*a[j,])
    ?这看起来对吗?嗨,格雷戈。它将是:(totPrimas[1,]*prod(a[1:j,])+(totPrimas[2,]*prod(a[2:j,])+…+(totPrimas[j,]*a[j,])。谢谢,我只是想确保我理解:结果的
    j
    第行条目将是
    (totPrimas[1,]*cumprod(a[1:j,])+(totPrimas[2,]*cumprod(a[2:j,])+…+(totPrimas[j,]*a[j,])
    ?这看起来对吗?嗨,格雷戈。它将是:(totPrimas[1,]*prod(a[1:j,])+(totPrimas[2,]*prod(a[2:j,])+…+(totPrimas[j,]*a[j,])。谢谢,我只是想确保我理解:结果的
    j
    第行条目将是
    (totPrimas[1,]*cumprod(a[1:j,])+(totPrimas[2,]*cumprod(a[2:j,])+…+(totPrimas[j,]*a[j,])
    ?这看起来对吗?嗨,格雷戈。它将是:(totPrimas[1,]*prod(a[1:j,])+(totPrimas[2,]*prod(a[2:j,])+…+(totPrimas[j,]*a[j,])。谢谢你!进展真的很快,我必须从Rcpp开始。谢谢完美的进展真的很快,我必须从Rcpp开始。谢谢完美的进展真的很快,我必须从Rcpp开始。谢谢完美的进展真的很快,我必须从Rcpp开始。谢谢