我正在尝试运行以下代码。坦率地说,我对 C++ 知之甚少,但我想让以下函数运行。你能帮我运行这个愚蠢的例子吗?
cppFunction(
'NumericVector abc(int x, int x_end, NumericVector y)
{
NumericVector z;
int x1 = x + x_end;
z = y[x:x1];
return(z);
}'
)
abc(3,c(0,1,10,100,1000,10000))
我看到这个...
错误:“:”标记之前应有“]”
Update抱歉,我忘了提到我需要从中生成一系列数字x
to x1
。功能IntegerVector::create
仅创建一个变量x
and x1
not x
though x1
。我举的例子很简单。我现在更新了这个例子。我需要做C++ what seq()
确实在R
基于以下答案的解决方案(@SleuthEye)
Rcpp::cppFunction(
'NumericVector abc(int x, int x_end, NumericVector y)
{
NumericVector z;
Range idx(x,x_end);
z = y[idx];
return(z);
}'
)
abc(3,5,c(0,1,10,100,1000,10000))
[1] 100 1000 10000