[R] calculating the mode in R...

2011-03-29 Thread Fernando Marmolejo Ramos
Dear R users

I’m aware that the package “modest” is useful to find the mode in an array.

However, I’d like to know if someone has translated the “mode” function built-in
in MATLAB into R language. This function finds the most frequent value in an
array (http://www.mathworks.com/help/techdoc/ref/mode.html).

Best

Fer

__
R-help@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] calculating the mode in R...

2011-03-30 Thread Carl Witthoft

Here's my modest contribution:


smode<-function(x){
xtab<-table(x)
modes<-xtab[max(xtab)==xtab]
mag<-as.numeric(modes[1]) #in case mult. modes, this is safer
themodes<-names(modes)
mout<-list(themodes=themodes,modeval=mag)
return(mout)
}

(I called it 'smode' because there's a 'mode' func somewhere which is 
related to the data type or something)


Carl

__
R-help@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.


Re: [R] calculating the mode in R...

2011-03-30 Thread Rainer M Krug
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 30/03/11 02:47, Fernando Marmolejo Ramos wrote:
> Dear R users
> 
> I?m aware that the package ?modest? is useful to find the mode in an array.
> 
> However, I?d like to know if someone has translated the ?mode? function 
> built-in
> in MATLAB into R language. This function finds the most frequent value in an
> array (http://www.mathworks.com/help/techdoc/ref/mode.html).

This sounds like a combination of the table() which tabulates your data,
and the max() function to identify the max - but then you have to
specify what to do when two values have the same count.

Rainer

> 
> Best
> 
> Fer
> 
> __
> R-help@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.


- -- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Natural Sciences Building
Office Suite 2039
Stellenbosch University
Main Campus, Merriman Avenue
Stellenbosch
South Africa

Tel:+33 - (0)9 53 10 27 44
Cell:   +27 - (0)8 39 47 90 42
Fax (SA):   +27 - (0)8 65 16 27 82
Fax (D) :   +49 - (0)3 21 21 25 22 44
Fax (FR):   +33 - (0)9 58 10 27 44
email:  rai...@krugs.de

Skype:  RMkrug
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk2S6TIACgkQoYgNqgF2ego48ACdHfCL+BdGA6wZ4bHrjq2wCXJW
vIoAnREiHFeSbJy9vYQPEnRhpV6nLbNB
=suk2
-END PGP SIGNATURE-

__
R-help@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.


Re: [R] calculating the mode in R...

2011-03-30 Thread Tal Galili
Hello Rainer and Fernando,

Actually, I think this function should involve the which.max (not max):

Here is a tiny function to perform this (with smarter handeling of multiple
modes and giving proper warning in such cases)
The.mode <- function(x, show_all_modes = F)
{
 x_freq <- table(x)
 mode_location <- which.max(x_freq)
 The_mode <- names(x_freq)[mode_location]
 Number_of_modes <- length(mode_location)
 #
 if(show_all_modes) {
  if(Number_of_modes >1) {
   warning(paste("Multiple modes exist - returning all",Number_of_modes,"of
them"))}
  return(The_mode)
 } else {
  if(Number_of_modes >1) {
   warning(paste("Multiple modes exist - returning only the first one out
of", Number_of_modes))}
  return(The_mode[1])
 }
}


Cheers,
Tal

Contact
Details:---
Contact me: tal.gal...@gmail.com |  972-52-7275845
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
--




On Wed, Mar 30, 2011 at 11:26 AM, Rainer M Krug  wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA1
>
> On 30/03/11 02:47, Fernando Marmolejo Ramos wrote:
> > Dear R users
> >
> > I?m aware that the package ?modest? is useful to find the mode in an
> array.
> >
> > However, I?d like to know if someone has translated the ?mode? function
> built-in
> > in MATLAB into R language. This function finds the most frequent value in
> an
> > array (http://www.mathworks.com/help/techdoc/ref/mode.html).
>
> This sounds like a combination of the table() which tabulates your data,
> and the max() function to identify the max - but then you have to
> specify what to do when two values have the same count.
>
> Rainer
>
> >
> > Best
> >
> > Fer
> >
> > __
> > R-help@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.
>
>
> - --
> Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
> Biology, UCT), Dipl. Phys. (Germany)
>
> Centre of Excellence for Invasion Biology
> Natural Sciences Building
> Office Suite 2039
> Stellenbosch University
> Main Campus, Merriman Avenue
> Stellenbosch
> South Africa
>
> Tel:+33 - (0)9 53 10 27 44
> Cell:   +27 - (0)8 39 47 90 42
> Fax (SA):   +27 - (0)8 65 16 27 82
> Fax (D) :   +49 - (0)3 21 21 25 22 44
> Fax (FR):   +33 - (0)9 58 10 27 44
> email:  rai...@krugs.de
>
> Skype:  RMkrug
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v1.4.10 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk2S6TIACgkQoYgNqgF2ego48ACdHfCL+BdGA6wZ4bHrjq2wCXJW
> vIoAnREiHFeSbJy9vYQPEnRhpV6nLbNB
> =suk2
> -END PGP SIGNATURE-
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] calculating the mode in R...

2011-03-30 Thread Rainer M Krug
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 30/03/11 10:59, Tal Galili wrote:
> Hello Rainer and Fernando,
>  
> Actually, I think this function should involve the which.max (not max):

Right - was not awarw of which.max - I would have used which(max(...)) -
which.max is much more elegant.

Thanks,

Rainer

>  
> Here is a tiny function to perform this (with smarter handeling of
> multiple modes and giving proper warning in such cases)
> The.mode <- function(x, show_all_modes = F)

It is advisable to use FaALSE instead of F, especially in a function:
you can assign values to F and T, but you can not assign values to FALSE
and TRUE

Cheers,

Rainer

> {
>  x_freq <- table(x)
>  mode_location <- which.max(x_freq)
>  The_mode <- names(x_freq)[mode_location]
>  Number_of_modes <- length(mode_location)
>  #
>  if(show_all_modes) {
>   if(Number_of_modes >1) {
>warning(paste("Multiple modes exist - returning
> all",Number_of_modes,"of them"))}
>   return(The_mode)
>  } else {
>   if(Number_of_modes >1) {
>warning(paste("Multiple modes exist - returning only the first one
> out of", Number_of_modes))}   
>   return(The_mode[1])
>  }
> }
>  
>  
> Cheers,
> Tal
> 
> Contact
> Details:---
> Contact me: tal.gal...@gmail.com  | 
> 972-52-7275845
> Read me: www.talgalili.com  (Hebrew) |
> www.biostatistics.co.il  (Hebrew) |
> www.r-statistics.com  (English)
> --
> 
> 
> 
> 
> On Wed, Mar 30, 2011 at 11:26 AM, Rainer M Krug  > wrote:
> 
> On 30/03/11 02:47, Fernando Marmolejo Ramos wrote:
>> Dear R users
> 
>> I?m aware that the package ?modest? is useful to find the mode in
> an array.
> 
>> However, I?d like to know if someone has translated the ?mode?
> function built-in
>> in MATLAB into R language. This function finds the most frequent
> value in an
>> array (http://www.mathworks.com/help/techdoc/ref/mode.html).
> 
> This sounds like a combination of the table() which tabulates your data,
> and the max() function to identify the max - but then you have to
> specify what to do when two values have the same count.
> 
> Rainer
> 
> 
>> Best
> 
>> Fer
> 
>> __
>> R-help@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@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.




- -- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Natural Sciences Building
Office Suite 2039
Stellenbosch University
Main Campus, Merriman Avenue
Stellenbosch
South Africa

Tel:+33 - (0)9 53 10 27 44
Cell:   +27 - (0)8 39 47 90 42
Fax (SA):   +27 - (0)8 65 16 27 82
Fax (D) :   +49 - (0)3 21 21 25 22 44
Fax (FR):   +33 - (0)9 58 10 27 44
email:  rai...@krugs.de

Skype:  RMkrug
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk2S9ccACgkQoYgNqgF2egqLtQCeM5ntWTHOy2wyk4CbOxwnUfJG
VZUAnRcOpRImBm/AbhsK14O54+pzCP6H
=QW4d
-END PGP SIGNATURE-

__
R-help@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.