[R] How to get the most frequent value of the subgroup

Milan Bouchet-Valat nalimilan at club.fr
Fri Mar 30 09:38:13 CEST 2012


Le jeudi 29 mars 2012 à 09:49 -0500, Yongsuhk Jung a écrit :
> 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.
It would have been better if you had provided a sample data as asked by
the posting guide.

Anyway, here's a possibility:
> df <- data.frame(a=rep(1:3, 2), b=c(1, 2, 2, 1, 1, 2))
> df
  a b
1 1 1
2 2 2
3 3 2
4 1 1
5 2 1
6 3 2
> aggregate(df$a, list(df$b), function(x) max(table(x)))
  Group.1 x
1       1 2
2       2 2


Regards



More information about the R-help mailing list