2017-04-14 19 views
1

調用C時當在R使用向量參數相關使用.Call.C是否有可訪問的矢量接口功能,我的當前的方法是處理類的長度,最大值等的某些屬性,在R再通這些屬性作爲C函數的參數。中的R

R extension起,至少有一個函數名稱length可用。那麼在CR之間是否有類似的接口,如max,min,rep等。

回答

2

Rcpp具有基本功能,如min,maxrep。請看下面的例子(假設這就是所謂的example.cpp):

#include <Rcpp.h> 
using namespace Rcpp; 

// [[Rcpp::export]] 
NumericVector exampleMinMax(NumericVector x) { 
    NumericVector out(2); 
    out[0] = min(x); 
    out[1] = max(x); 
    return out; 
} 

// [[Rcpp::export]] 
NumericVector exampleRep(NumericVector x, int n) { 
    NumericVector out = rep_each(x, n); 
    return out; 
} 

然後在R你可以這樣做:

library(Rcpp) 
sourceCpp("example.cpp") 
exampleMinMax(1:10) 

[1] 1 10 

exampleRep(1:10, 2) 

[1] 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 
+0

談到RCPP是合理的。 –