From spencer.graves at PDF.COM Mon Jun 23 14:36:45 2003
Received: from postal.pdf.com (pdf193.pdf.com [209.128.81.193]) by uhddx01.dt.uh.edu (8.9.2/8.9.2) with ESMTP id OAA09671 for <hodgess at uhddx01.dt.uh.edu>; Mon, 23 Jun 2003 14:36:44 -0500 (CDT) Received: from postage.pdf.com (postage.pdf.com [10.10.8.7]) by postal.pdf.com (Switch-3.0.4/Switch-3.0.0) with ESMTP id h5NJYisf002096; Mon, 23 Jun 2003 12:34:44 -0700 (PDT) Received: from malt.PDF.COM (malt.pdf.com [10.10.8.80]) by postage.pdf.com (Switch-3.1.0/Switch-3.1.0) with ESMTP id h5NJZw4U022559; Mon, 23 Jun 2003 12:35:58 -0700 (PDT) Received: from pdf.com (sjc-10-10-11-97.pdf.com [10.10.11.97]) by malt.PDF.COM (8.9.3/8.9.3) with ESMTP id MAA24847; Mon, 23 Jun 2003 12:36:05 -0700 (PDT) Message-ID: <3EF756A5.9040008 at pdf.com> Date: Mon, 23 Jun 2003 12:36:05 -0700 From: Spencer Graves <spencer.graves at PDF.COM> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20030208 Netscape/7.02 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Ravi Varadhan <rvaradha at jhsph.edu> CC: Erin Hodgess <hodgess at uhddx01.dt.uh.edu>, r-help at stat.math.ethz.ch Subject: Re: [R] Summary for mode of a data set References: <c3910ac38114.c38114c3910a at jhsph.edu> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Status: R You are correct that my function is also wrong. You need to decide what you want and write a function to do that, if you don't have it already. Best Wishes, Spencer Graves
Ravi Varadhan wrote:
Dear Spencer: In the following example, your code doesn't pick up the local mode at 5.
x2 <- c(1,1,2,3,3,3,3,5,5,5) modes(x2)
[1] 1 3 In this example, it gives a mode at 7, which is incorrect.
x2 <- c(1,1,2,3,3,3,3,5,5,5,6,7) modes(x2)
[1] 1 3 7 Ravi. ----- Original Message ----- From: Spencer Graves <spencer.graves at pdf.com> Date: Monday, June 23, 2003 2:53 pm Subject: Re: [R] Summary for mode of a data set
Your "mode1" function will identify multiple modes only if they have the same number of observations. Consider the following:
x2 <- c(2, 1,1, 3,3,3) mode1(x2)
[1] 3
Here, "mode1" did not identify the local mode at 1, because it had
fewer
observations than 3. If you want the modes at both 1 and 3, then
consider the following:
modes <- function(x){
xt <- table(x)
nt <- length(xt)
sel <- c(xt[-nt]>=xt[-1], T)&c(T, xt[-1]>=xt[-nt])
as.numeric(names(xt[sel]))
}
modes(x2)
[1] 1 3 hth. spencer graves Erin Hodgess wrote:
Dear R People: thank you for the many helpful sets of code that I received!!! I combined several of the concepts for the following function:
mode1
function(x) {
y <- rle(sort(x))
z <- y$values[y$lengths==max(y$lengths)]
return(z)
}
xm
[1] 22 15 10 30 25 26 2 17 28 2 24 6 26 24 5 22 20 14
mode1(xm)
[1] 2 22 24 26 This will pick up multiple modes. Again thanks to all who helped! Sincerely, Erin mailto: hodgess at uhddx.01.dt.uh.edu
______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help