Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent function in C/C++ to GNU-R which()?

Tags:

c++

c

r

Let me explain what the 'which' function does:

From GNU-R help:

which indices are TRUE?

Give the ‘TRUE’ indices of a logical object, allowing for array indices.

or showing some code: (GNU-R starts counting indices with 1)

> x <- c(1,2,3,1,3,5);
> which(x == 1);
[1] 1 4
> which(x == 3);
[1] 3 5
> ll <- c(TRUE,FALSE,TRUE,NA,FALSE,FALSE,TRUE);
> which(ll);
[1] 1 3 7

Does anyone know a similar function in C/C++?

Thanks for your help

rinni

like image 815
rinni Avatar asked May 23 '11 13:05

rinni


1 Answers

You have to understand that R is vectorised, whereas C first and foremost works on individual atomistic data pieces: a single int, double, ...

With C++, you can look into STL algorithms with which you approach this.

Lastly, at the R and C++ intersection, our Rcpp package has some vectorized operations in C++ which mimic some operations; see the Rcpp-sugar pdf vignette for more (and/or some of our talks on Rcpp).

like image 157
Dirk Eddelbuettel Avatar answered Oct 04 '22 22:10

Dirk Eddelbuettel