Rcpp で R コードを最適化する
Romain François
Consulting Datactive, ThinkR
Rcppのベクトルクラス:
NumericVector:numericベクトル(例:c(1,2,3))IntegerVector:integer(例:1:3)LogicalVector:logical(例:c(TRUE, FALSE))CharacterVector:文字列(例:c("a", "b", "c"))その他:
List:任意のRオブジェクトのリスト主なメソッド:
x.size():ベクトルxの要素数を返すx[i]:ベクトルxのi番目の要素を返すC++のインデックスは0始まりです。インデックスは先頭からのオフセットを表します。
// first element of the vector
x[0]
// last element
x[x.size()-1]
Rのインデックスは1始まりです。
# first
x[1]
# last
x[length(x)]

// x comes from somewhere else (patience ...)
NumericVector x = ... ;
double value = x[0] ;
x[0] = 12.0 ;
// x comes from somewhere else (patience ...)
NumericVector x = ... ;
int n = x.size() ;
double value = x[n-1] ;
x[n-1] = 12.0 ;
// x comes from somewhere
NumericVector x = ... ;
int n = x.size() ;
for( int i=0; i<n; i++){
// manipulate x[i]
}
Rcpp で R コードを最適化する