[R] "mode" function

David Winsemius dwinsemius at comcast.net
Wed Sep 1 06:10:30 CEST 2010


On Aug 31, 2010, at 10:48 PM, Dejian Zhao wrote:

> R does have a mode function, but it seems NOT to do the same thing  
> as in matlab.

Correct. It does not return the value of a vector with maximal  
proportions of individual elements.

?mode

(It appears that the Matlab formalism fn(Name[vec, :]) may be similar  
to the apply function, but that is just a guess from what was written.  
There is an "R for Matlab Users" document by David Hiebeler that  
should be easy to find with a Google search. Probably also in the  
contributed docs.)
-- 
David

>
> > A <- matrix(c(1:3,1,3,2,3,3,2),nrow=3,byrow=F)
> > A
>     [,1] [,2] [,3]
> [1,]    1    1    3
> [2,]    2    3    3
> [3,]    3    2    2
> > A[,2]
> [1] 1 3 2
> > mode(A)
> [1] "numeric"
> > mode(A[,2])
> [1] "numeric"
>
>
> On 2010-9-1 5:19, Iain Martyn wrote:
>> Hi, I have what I think is a simple question/issue but I have  
>> unable to find the answer for it either in the R-manual or by  
>> browsing the web.
>>
>>
>>
>> I would like to know if there is a "mode" function in R, such that  
>> from a vector or matrix the function returns the most common  
>> value.  In other programs I use (such as Matlab) I can have for  
>> exampe a 3X3 matrix A, A=[1 2 3; 1 3 2; 3 3 2] and I can find the  
>> mode of the rows so that mode(A[1:3,:]) returns a 1X3 matrix [1 3 2].
>>
>> Is this possible in R as well?  If not, how would one go about this?
>>
>> Thank you very much,
>>
>> Bill
>>  		 	   		
>> 	[[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.
>>
>
> ______________________________________________
> 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