[R] Mode value

Jonathan Baron baron at psych.upenn.edu
Sat Sep 6 20:37:39 CEST 2008


On 09/06/08 17:24, Carlos Morales wrote:
> Hello everyone,
> 
> 
> I would like to know if there is any function to calculate the mode value, or I have 
> to build one to do it.

I just did this the other day.  Funny you should ask.  It finds the
mode of each row of a matrix called Pbest.

Pbest.p <- apply(Pbest,1,function(x) 
 x[which.max(as.vector(table(x)))][1])

So to get the mode of a vector v1, the following should work (but I
haven't tried it):

v1[which.max(as.vector(table(v1)))][1]

-- 
Jonathan Baron, Professor of Psychology, University of Pennsylvania
Home page: http://www.sas.upenn.edu/~baron



More information about the R-help mailing list