Dear R users I have a very basic question. I tried but could not find the required result. using dat <- pima f <- table(dat[,9])> f0 1 500 268 i want to find that class say "0" having maximum frequency i.e 500. I used>which.max(f)which provide 0 1 How can i get only the "0". Thanks and best regards Muhammad Azam Ph.D. Student Department of Medical Statistics, Informatics and Health Economics University of Innsbruck, Austria [[alternative HTML version deleted]]
On 6/6/2008 9:14 AM, Muhammad Azam wrote:> Dear R users > I have a very basic question. I tried but could not find the required result. using > dat <- pima > f <- table(dat[,9]) > >> f > 0 1 > 500 268 > i want to find that class say "0" having maximum frequency i.e 500. I used >> which.max(f) > which provide > 0 > 1 > How can i get only the "0". Thanks andtable(iris$Species) setosa versicolor virginica 50 50 50 which.max(table(iris$Species)) setosa 1 names(which.max(table(iris$Species))) [1] "setosa"> best regards > > Muhammad Azam > Ph.D. Student > Department of Medical Statistics, > Informatics and Health Economics > University of Innsbruck, Austria > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
The 0 is the name of the item and the 1 is the index in f of the maximum class. (since f is a table, and the first element of the table is the maximum, which.max returns a 1) So, if you just want to know which class is maximum you can say names(which.max(f)) Michael Conklin Chief Methodologist - Advanced Analytics MarketTools, Inc. 6465 Wayzata Blvd. Suite 170 Minneapolis, MN 55426 Tel: 952.417.4719 | Mobile:612.201.8978 Michael.Conklin at markettools.com MarketTools(r) http://www.markettools.com This e-mail and any attachments may contain privileged, confidential or proprietary information. If you are not the intended recipient, be aware that any review, copying, or distribution of this e-mail or any attachment is strictly prohibited. If you have received this e-mail in error, please return it to the sender immediately, and permanently delete the original and any copies from your system. Thank you for your cooperation. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Muhammad Azam Sent: Friday, June 06, 2008 8:15 AM To: R Help; R-help request Subject: [R] request: a class having max frequency Dear R users I have a very basic question. I tried but could not find the required result. using dat <- pima f <- table(dat[,9])> f0 1 500 268 i want to find that class say "0" having maximum frequency i.e 500. I used>which.max(f)which provide 0 1 How can i get only the "0". Thanks and best regards Muhammad Azam Ph.D. Student Department of Medical Statistics, Informatics and Health Economics University of Innsbruck, Austria [[alternative HTML version deleted]] ______________________________________________ R-help at 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.
names(f)[which.max(f)] on 06/06/2008 09:14 AM Muhammad Azam said the following:> Dear R users > I have a very basic question. I tried but could not find the required result. using > dat <- pima > f <- table(dat[,9]) > >> f > 0 1 > 500 268 > i want to find that class say "0" having maximum frequency i.e 500. I used >> which.max(f) > which provide > 0 > 1 > How can i get only the "0". Thanks and > > > best regards > > Muhammad Azam > Ph.D. Student > Department of Medical Statistics, > Informatics and Health Economics > University of Innsbruck, Austria > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >
On 6/6/2008 9:18 AM, Chuck Cleland wrote:> On 6/6/2008 9:14 AM, Muhammad Azam wrote: >> Dear R users >> I have a very basic question. I tried but could not find the >> required result. using >> dat <- pima >> f <- table(dat[,9]) >> >>> f >> 0 1 500 268 >> i want to find that class say "0" having maximum frequency i.e 500. I >> used >>> which.max(f) >> which provide 0 1 How can i get only the "0". Thanks and > > table(iris$Species) > > setosa versicolor virginica > 50 50 50 > > which.max(table(iris$Species)) > setosa > 1 > > names(which.max(table(iris$Species))) > [1] "setosa"If, as above, more than one category frequency is at the maximum, you might want something like this: x <- table(iris$Species) which(x == max(x)) setosa versicolor virginica 1 2 3 names(which(x == max(x))) [1] "setosa" "versicolor" "virginica">> best regards >> >> Muhammad Azam Ph.D. Student Department of Medical Statistics, >> Informatics and Health Economics University of Innsbruck, Austria >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at 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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Muhammad Azam wrote:> Dear R users > I have a very basic question. I tried but could not find the required result. using > dat <- pima > f <- table(dat[,9]) > > >>f > > 0 1 > 500 268 > i want to find that class say "0" having maximum frequency i.e 500. I used > >>which.max(f) > > which provide > 0 > 1 > How can i get only the "0". Thanks and >The "Mode" function in the prettyR package does this. Jim