Jung, Yongsuhk
2012-Mar-29 13:30 UTC
[R] How to get the most frequent value of the subgroup
Dear Members of the R-Help, While using a R function - 'aggregate' that you developed, I become to have a question. In that function,> aggregate(x, by, FUN, ..., simplify = TRUE)I was wondering about what type of FUN I should write if I want to get "the most frequent value of the subgroup" as a summary statistics of the subgroups. I will appreciate if I can get your idea on this issue. Many thanks, Jason Jung. [[alternative HTML version deleted]]
Yongsuhk Jung
2012-Mar-29 14:49 UTC
[R] How to get the most frequent value of the subgroup
Dear Members of the R-Help, While using a R function - 'aggregate' that you developed, I become to have a question. In that function,> aggregate(x, by, FUN, ..., simplify = TRUE)I was wondering about what type of FUN I should write if I want to get "the most frequent value of the subgroup" as a summary statistics of the subgroups. I will appreciate if I can get your idea on this issue. Many thanks, Jason Jung. [[alternative HTML version deleted]]
R. Michael Weylandt
2012-Mar-29 15:01 UTC
[R] How to get the most frequent value of the subgroup
What you are looking for is commonly called the mode, but (surprisingly?) there doesn't seem to be a standard implementation for mode() in R. (the mode function gives you something else entirely) -- this SO thread may be of help: http://stackoverflow.com/questions/2547402/standard-library-function-in-r-for-finding-the-mode Michael On Thu, Mar 29, 2012 at 9:30 AM, Jung, Yongsuhk <yjung.phd at ivey.ca> wrote:> Dear Members of the R-Help, > > > While using a R function - 'aggregate' that you developed, I become to have a question. > > In that function, > > > >> aggregate(x, by, FUN, ..., simplify = TRUE) > > > > I was wondering about what type of FUN I should write if I want to get "the most frequent value of the subgroup" as a summary statistics of the subgroups. > > I will appreciate if I can get your idea on this issue. > > Many thanks, > > Jason Jung. > > > ? ? ? ?[[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.
David Winsemius
2012-Mar-29 15:11 UTC
[R] How to get the most frequent value of the subgroup
On Mar 29, 2012, at 9:30 AM, Jung, Yongsuhk wrote:> Dear Members of the R-Help, > > > While using a R function - 'aggregate' that you developed, I become > to have a question. > > In that function, > >> aggregate(x, by, FUN, ..., simplify = TRUE) > > I was wondering about what type of FUN I should write if I want to > get "the most frequent value of the subgroup" as a summary > statistics of the subgroups.Something along the lines of function(x){ tbl <- table(x); retrun(names(tbl)[which.max(tbl)] } You could also search for definitions that have appeared over the years in rhelp for a mode function (but not the existing mode function which is not designed to deliver what you request.)> > [[alternative HTML version deleted]]-- David Winsemius, MD West Hartford, CT