[Rcpp-devel] Rcpp "version" of R's match function

Romain Francois romain at r-enthusiasts.com
Thu Nov 15 00:01:05 CET 2012

Le 14/11/12 23:48, Søren Højsgaard a écrit :
> Dear all,
> I need to call R's match function from a c++ program so I can do
>   Rcpp::Function R_match("match");
>   Rcpp::NumericVector out = R_match(x_, table_);
> However, I wonder if there is such a "match" function in C++ that I can call (to avoid the extra overhead)?
> Any ideas would be welcome
> Regards
> Søren

We don't have one in Rcpp. It would not be too hard I guess to use some 
algorithms from the STL, e.g. std::find.

Not easy to say more withou seeing exactly what you are trying to do. 
For example, do you need to store "out" or is it enough to work one 
value at a time ...

Also, R's match returns an integer vector, so you probably want:

Rcpp::IntegerVector out = R_match(x_, table_);

otherwise you also pay for making the coercion to numeric on top of the 
price you pay for calling back into R.

Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

R Graph Gallery: http://gallery.r-enthusiasts.com
`- http://bit.ly/SweN1Z : SuperStorm Sandy

blog:            http://romainfrancois.blog.free.fr
|- http://bit.ly/RE6sYH : OOP with Rcpp modules
`- http://bit.ly/Thw7IK : Rcpp modules more flexible

More information about the Rcpp-devel mailing list