Quantcast

Mode (statistics) in R?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Mode (statistics) in R?

JustADude
Hopefully this is a pretty simple question:
 
Is there a function in R that calculates the "mode" of a sample?   That is, I would like to be able to determine the value that occurs the most frequently in a data set.
 
I tried the default R "mode" function, but it appears to provide a storage type or something else. 
 
I tried the RSeek and some R documentation that I downloaded, but nothing seems to mention calculating the "mode".
 
Thanks again.
 
 


     
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mode (statistics) in R?

Carlos J. Gil Bellosta
Hello,

You can try ?table.

Best regards,

Carlos J. Gil Bellosta
http://www.datanaytics.com

On Mon, 2009-01-26 at 05:28 -0800, Jason Rupert wrote:

> Hopefully this is a pretty simple question:
>
> Is there a function in R that calculates the "mode" of a sample? That is, I would like to be able to determine the value that occurs the most frequently in a data set.
>
> I tried the default R "mode" function, but it appears to provide a storage type or something else.
>
> I tried the RSeek and some R documentation that I downloaded, but nothing seems to mention calculating the "mode".
>
> Thanks again.
>
>
>
>
>      
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mode (statistics) in R?

Mike Lawrence-7
In reply to this post by JustADude
Here's a rather convoluted way of finding the mode (or, at least, the
first mode):

x = round(rnorm(100,sd=5))
my_mode = as.numeric(names(table(x))[which.max(table(x))])




On Mon, Jan 26, 2009 at 9:28 AM, Jason Rupert <[hidden email]> wrote:

> Hopefully this is a pretty simple question:
>
> Is there a function in R that calculates the "mode" of a sample?   That is, I would like to be able to determine the value that occurs the most frequently in a data set.
>
> I tried the default R "mode" function, but it appears to provide a storage type or something else.
>
> I tried the RSeek and some R documentation that I downloaded, but nothing seems to mention calculating the "mode".
>
> Thanks again.
>
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> [hidden email] 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.
>
>



--
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University
www.thatmike.com

Looking to arrange a meeting? Check my public calendar:
http://www.thatmike.com/mikes-public-calendar

~ Certainty is folly... I think. ~

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mode (statistics) in R?

JustADude
Thanks.
 
I ended up breaking it up into two steps:
 
table_data<-table(data)
subset(table_data, table_data==max(table_data))
 
Thanks again.


--- On Mon, 1/26/09, Mike Lawrence <[hidden email]> wrote:

From: Mike Lawrence <[hidden email]>
Subject: Re: [R] Mode (statistics) in R?
To: [hidden email]
Cc: [hidden email]
Date: Monday, January 26, 2009, 7:39 AM

Here's a rather convoluted way of finding the mode (or, at least, the
first mode):

x = round(rnorm(100,sd=5))
my_mode = as.numeric(names(table(x))[which.max(table(x))])




On Mon, Jan 26, 2009 at 9:28 AM, Jason Rupert <[hidden email]>
wrote:
> Hopefully this is a pretty simple question:
>
> Is there a function in R that calculates the "mode" of a sample?
  That is, I would like to be able to determine the value that occurs the most
frequently in a data set.
>
> I tried the default R "mode" function, but it appears to provide
a storage type or something else.
>
> I tried the RSeek and some R documentation that I downloaded, but nothing
seems to mention calculating the "mode".

>
> Thanks again.
>
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> [hidden email] 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.
>
>



--
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University
www.thatmike.com

Looking to arrange a meeting? Check my public calendar:
http://www.thatmike.com/mikes-public-calendar

~ Certainty is folly... I think. ~



     
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mode (statistics) in R?

Marc Schwartz
In reply to this post by JustADude
on 01/26/2009 07:28 AM Jason Rupert wrote:
> Hopefully this is a pretty simple question:
> �
> Is there a function in R that calculates the "mode" of a sample?�� That is, I would like to be able to determine the value that occurs the most frequently in a data set.
> �
> I tried the default R "mode" function, but it appears to provide a storage type or something else.�
> �
> I tried the RSeek and some R documentation that I downloaded, but nothing seems to mention calculating the "mode".
> �
> Thanks again.

It depends upon the type of data you are dealing with.

If it is discrete, you can use table() to calculate frequencies and then
take the max:

set.seed(1)

tl <- table(sample(letters, 100, replace = TRUE))

> tl

a b c d e f g h i j k l m n o p q r s t u v w x y z
2 3 3 3 2 4 6 1 6 5 6 4 7 2 2 2 5 4 5 3 8 4 5 4 3 1

> tl[which.max(tl)]
u
8


Alternatively, if the data is continuous, then you will need to look at
some form of density estimation. There have been various discussions
over the years on how to go about doing this, but a simplistic approach
would be:

  set.seed(1)

  x <- rnorm(100)

  dx <- density(x)

  > dx$x[which.max(dx$y)]
  [1] 0.3294585


  # Review plot
  plot(dx)
  abline(v = dx$x[which.max(dx$y)])


See ?table, ?which.max and ?density

HTH,

Marc Schwartz

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mode (statistics) in R?

patricia garcía gonzález
In reply to this post by Carlos J. Gil Bellosta

Hello,
 
I think this will work:
 
names( sort( -table( x ) ) )[1]
 
Regards
 
Patricia García
 
> From: [hidden email]> To: [hidden email]> Date: Mon, 26 Jan 2009 18:34:00 +0500> CC: [hidden email]> Subject: Re: [R] Mode (statistics) in R?> > Hello,> > You can try ?table. > > Best regards,> > Carlos J. Gil Bellosta> http://www.datanaytics.com> > On Mon, 2009-01-26 at 05:28 -0800, Jason Rupert wrote:> > Hopefully this is a pretty simple question:> > > > Is there a function in R that calculates the "mode" of a sample? That is, I would like to be able to determine the value that occurs the most frequently in a data set. > > > > I tried the default R "mode" function, but it appears to provide a storage type or something else. > > > > I tried the RSeek and some R documentation that I downloaded, but nothing seems to mention calculating the "mode". > > > > Thanks again.> > > > > > > > > > > > [[alternative HTML version deleted]]> > > > ______________________________________________> > [hidden email] 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.> > ______________________________________________> [hidden email] 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.
_________________________________________________________________


        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Loading...