-1
我得到 “错误:索引越界” 当我使用mapply具有RCPP功能:RCPP错误:索引越界
R:
mapply(fun, x = totPrimas, y = factorProjec, w = totCurvadf)
x,y和z是数据尺寸相同的框架。
RCPP:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector fun(const NumericVector x, const NumericVector y, const NumericVector w) {
NumericVector z(x.size());
z(0) = x(0) * y(0);
NumericVector c(x.size());
c(0) = x(0) * w(0);
for(int i = 1; i < x.size(); i++) {
c(i) = (c(i-1) + x(i)) * w(i);
z(i) = c(i) * y(i);
}
return z;
}
是一些错误的代码?非常感谢。
我不明白“x,y和z是数据与同一帧维度“。这与你的代码有什么关系? – Roland
我看不到索引超出了C++代码的范围,所以,这个问题可能是你通过'mapply'传递给'fun'的原因。为了澄清 – Roland
谢谢,我检查了输入,你是对的。“w”是一个矩阵,我将它转换为data.frame并且工作正常,谢谢! –