[R] Measures of central tendency - mode

Patrick E. McKnight pem at theriver.com
Fri Jan 30 23:11:00 CET 2004


Thanks to Andy Liaw and J.R. Lockwood for your suggestions.  The which.max() worked great along with the rownames.  The complete solution for me was:

a <- table(varname)
> my.mode <- rownames(a)[which.max(a)]
> my.mode
[1] "1"

Amazing how a simple concept such as mode can present problems for us.  Thanks again.

To reply to Spencer Graves' question, I didn't find the disucssion via search.  I guess I might have overlooked the thread if it were titled kernel density since that seemed far too technical for this basic topic.  Sorry if I cluttered up the list though.

Cheers,

Patrick


On Fri, 30 Jan 2004 16:47:54 -0500 (EST)
"J.R. Lockwood" <lockwood at rand.org> wrote:

> it is an annoyance that table() provides the values being tables as
> the rownames of the resultant vector.  you can do something like:
> 
> a<-table(x)
> rownames(a)[which.max(a)]
> 
> On Fri, 30 Jan 2004, Patrick E. McKnight wrote:
> 
> > Date: Fri, 30 Jan 2004 13:55:19 -0700
> > From: Patrick E. McKnight <pem at theriver.com>
> > To: "r-help at stat.math.ethz.ch" <r-help at stat.math.ethz.ch>
> > Subject: [R] Measures of central tendency - mode
> > 
> > Greetings,
> > 
> > This seems too rudimentary to ask but for the life of me I cannot locate a readily easy method to compute the univariate mode.  I know "mode" is not correct and "table" provides a reasonable count but I figured there would be an easy way to extract the value from the table after I do something like:
> > 
> > max(table(mydadat$myvar))
> > 
> > unfortunately it only returns the max count and not the value that is observed most.  Would some kind soul help me out with this seemingly trivial problem?
> > 
> > Thanks in advance.
> > 
> > Cheers,
> > 
> > Patrick
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> > 
> 
> J.R. Lockwood
> 412-683-2300 x4941
> lockwood at rand.org
> http://www.rand.org/methodology/stat/members/lockwood/
>




More information about the R-help mailing list