It may be possible to give a solution without a single for loop.

set.seed(1)
v<-sample(1:10,size=1e6,replace=TRUE)
p<-2:4


countinstance<-function(v,p) {
    res<-outer(v,p,FUN="==");
    apply(res,2,sum)

}

> system.time(replicate(50,countinstance(v,p)))/50
   user  system elapsed 
 0.2146  0.0248  0.2403 


It is ~50% slower than the "f2" solution given previously ... but it also gives 
you a 2D of where the matches are.
These are stored in the "res" variable; use with care with very big datasets.

I wonder whether it is possible to reduce the memory footprint with bit level 
operations ...


Christos Argyropoulos

                                          
_________________________________________________________________
Hotmail: Free, trusted and rich email service.

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to