Skip to content
Prev 360341 / 398503 Next

Finding Highest value in groups

Base R functions can handle this easily. It is preferable to use dput() as a compact way of providing data on the list:
+ 1        0.69
+ 1        0.31
+ 2        0.01
+ 2        0.99
+ 3        1.00
+ 4        NA
+ 4        0
+ 4        1
+ 5        0.5
+ 5        0.5
+ ", header=TRUE)
structure(list(ID = c(1L, 1L, 2L, 2L, 3L, 4L, 4L, 4L, 5L, 5L), 
    Value = c(0.69, 0.31, 0.01, 0.99, 1, NA, 0, 1, 0.5, 0.5)), .Names = c("ID", 
"Value"), class = "data.frame", row.names = c(NA, -10L))

Then you just need the aggregate() function:
ID Value
1  1  0.69
2  2  0.99
3  3  1.00
4  4  1.00
5  5  0.50

See ?aggregate for the help page.

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352


-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Tom Wright
Sent: Friday, April 22, 2016 9:13 AM
To: Saba Sehrish
Cc: R-help Mailing List
Subject: Re: [R] Finding Highest value in groups

Assuming your dataframe is in a variable x:

        
On Fri, 2016-04-22 at 13:51 +0000, Saba Sehrish via R-help wrote:
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.