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

Allan Engelhardt allane at cybaea.com
Fri Jun 19 17:51:31 CEST 2009


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=length(v[v==x]), sum=sum(v==x))
    test elapsed
3   sum   2.513
2 index   5.512
1 which   6.712
 > v<-rep(1:1000,1:1000); x<-5; benchmark(replications=200, 
columns=c("test","elapsed"), order="elapsed", which=length(which(x==v)), 
index=length(v[v==x]), sum=sum(v==x))
    test elapsed
3   sum   2.502
2 index   3.779
1 which   6.650
 > v<-rep(1:1000,1:1000); x<-5; benchmark(replications=200, 
columns=c("test","elapsed"), order="elapsed", which=length(which(x==v)), 
index=length(v[v==x]), sum=sum(v==x))
    test elapsed
2 index   3.796
3   sum   5.808
1 which   6.633

This pattern appears to repeat (so on the next two runs "sum" will win 
followed by "index" followed by "sum" twice followed by "index" ...)

Allan.




On 19/06/09 14:55, Praveen Surendran wrote:
> 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 while dealing with a large vector.
>
> Please have your comments.
>
>
>
> Praveen Surendran.
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>




More information about the R-help mailing list