[R] row index for max values of row groups

Rui Barradas ruipbarradas at sapo.pt
Wed Nov 7 20:12:56 CET 2012


Hello,

Though my function is equal to Arun's, it's wrapped by a different way 
of returning the index.

which(unlist(tapply(df1$values, df1$groups, FUN=function(x) x == max(x))))


Hope this helps,

Rui Barradas
Em 07-11-2012 18:54, arun escreveu:
> Hi,
> One method will be:
> row.names(df1[unlist(tapply(df1$values,df1$groups,FUN=function(x) x==max(x))),])
> #[1] "4"  "8"  "11"
> #or
> row.names(df1[as.logical(ave(df1$values,df1$groups,FUN=function(x) x==max(x))),])
> #[1] "4"  "8"  "11"
> A.K.
>
>
>
>
> ----- Original Message -----
> From: Omphalodes Verna <omphalodes.verna at yahoo.com>
> To: "r-help at r-project.org" <r-help at r-project.org>
> Cc:
> Sent: Wednesday, November 7, 2012 1:41 PM
> Subject: [R] row index for max values of row groups
>
> Dear list members!
> I am looking for ''nice solution'' for (maybe) simple problem. I need a code (small program) to calculate row index for max value (example below: df1$values) by groups (example below: df1$groups).
> df1 <- data.frame(
> groups = c(1,1,1,1,1,2,2,2,3,3,3,3),
> values = c(1,1,1,2,1,1,2,3,2,1,4,3)
> )
> df1
>
> expected results
>
>> 4 8 11 # row index of max values by group
> Thansk to all for help, OV
>
>      [[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.
>
>
> ______________________________________________
> 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.




More information about the R-help mailing list