[R] row index for max values of row groups
arun
smartpink111 at yahoo.com
Wed Nov 7 19:54:01 CET 2012
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.
More information about the R-help
mailing list