[R] [Fwd: modes again]

Gregor GORJANC gregor.gorjanc at bfro.uni-lj.si
Fri Nov 26 10:45:39 CET 2004


Hello!

Ross thanks for the code. I have made a comparison with SAS, and your 
code (simpleMode, which is also pasted bellow) works well for 
categorical as well as for continous data. However there are problems 
with multiple modes.

I have sent this mail also to the R-help list so everybody can benefit. 
I hope you do not mind.

---------------------------------------------------------------

#returns the lowest mode of a value set
#finds the mode of a data set using the sort function
#call with source simplemode(var)

simpleMode=function(var)
{
#create a table with the values sorted by frequency
sortvar=sort(table(var))

#put the most freq value (the last one) and its count into a variable
modeTable=sortvar[length(sortvar)]

#get the name (value) of the most frequent value
themoderesult=names(modeTable)

#get the count of the most frequent value (one could use min or max, but 
since there is only one, this method is simpler)
maxfreq=as.vector(modeTable)

cat("Mode: ",themoderesult, "(frequency: ",maxfreq,")")
}

-------- Original Message --------
Subject: modes again
Date: Mon, 22 Nov 2004 08:49:54 -0800
From: Ross Nelson <rnelson at cariboo.bc.ca>
To: gregor.gorjanc at bfro.uni-lj.si

Attached are two short functions to generate modes.

Place them in your working directory.

call the simpleMode file with

source("simpleMode")
simpleMode(var)

where var is a vector variable.


call the mode2 file with

source("mode2")
mode2(var)

where var is a vector variable.


A description of the mode2 function is available from
http://cran.r-project.org/doc/contrib/Lemon-kickstart/kr_dstat.html

I wrote the other.


-- 
Lep pozdrav / With regards / Con respeto,
     Gregor GORJANC

---------------------------------------------------------------
University of Ljubljana
Biotechnical Faculty       URI: http://www.bfro.uni-lj.si
Zootechnical Department    mail: gregor.gorjanc <at> bfro.uni-lj.si
Groblje 3                  tel: +386 (0)1 72 17 861
SI-1230 Domzale            fax: +386 (0)1 72 41 005
Slovenia




More information about the R-help mailing list