Re: [R] Alternate ways of finding number of occurrence of an element in a vector.

2009-06-21 Thread David Winsemius
If one puts the gc() call prior to the expressions themselves, one gets consistently ... different results: library("rbenchmark") v<-rep(1:500,1:500); x<-5; benchmark( which= c(gc(),length(which(x==v))), index= c(gc(), length(v[v==x])), sum= c(gc(), sum(v==x)), replications=200,

Re: [R] Alternate ways of finding number of occurrence of an element in a vector.

2009-06-21 Thread Allan Engelhardt
Answering my own question: if I explicitly garbage collecte before the benchmark then 'index' always wins, which probably also answers the original question. v<-rep(1:1000,1:1000); x<-5; gc(); benchmark(replications=200, columns=c("test","elapsed"), order="elapsed", which=length(which(x==v)),

Re: [R] Alternate ways of finding number of occurrence of an element in a vector.

2009-06-19 Thread Allan Engelhardt
When trying out a couple of different approaches to this problem I get rather different answers between runs. Anybody know why? > library("rbenchmark") > v<-rep(1:1000,1:1000); x<-5; benchmark(replications=200, columns=c("test","elapsed"), order="elapsed", which=length(which(x==v)), index=len

[R] Alternate ways of finding number of occurrence of an element in a vector.

2009-06-19 Thread Praveen Surendran
Hi, I have a vector "v" and would like to find the number of occurrence of element "x" in the same. Is there a way other than, sum(as.integer(v==x)) or length(which(x==v)) to do the this. I have a huge file to process and do this. Both the above described methods are pretty slow w