Re: [R] Finding Highest value in groups
Thanks a lot. Its really helpful Regards Saba On Saturday, 23 April 2016, 6:50, Giorgio Garziano wrote: Since the aggregate S3 method for class formula already has got na.action = na.omit, ## S3 method for class 'formula' aggregate(formula, data, FUN, ..., subset, na.action = na.omit) I think that to deal with NA's, it is enough: aggregate(Value~ID, dta, max) Moreover, passing na.rm = FALSE/TRUE is "don't care": aggregate(Value~ID, dta, max, na.rm=FALSE) result is: ID Value 1 1 0.69 2 2 0.99 3 3 1.00 4 4 1.00 5 5 0.50 which is the same of na.rm=TRUE. On the contrary, in the following cases: aggregate(Value~ID, dta, max, na.action = na.pass) ID Value 1 1 0.69 2 2 0.99 3 3 1.00 4 4NA 5 5 0.50 aggregate(Value~ID, dta, max, na.action = na.fail) Error in na.fail.default(list(Value = c(0.69, 0.31, 0.01, 0.99, 1, NA the result is different. -- Best, GG [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] Finding Highest value in groups
Since the aggregate S3 method for class formula already has got na.action = na.omit, ## S3 method for class 'formula' aggregate(formula, data, FUN, ..., subset, na.action = na.omit) I think that to deal with NA's, it is enough: aggregate(Value~ID, dta, max) Moreover, passing na.rm = FALSE/TRUE is "don't care": aggregate(Value~ID, dta, max, na.rm=FALSE) result is: ID Value 1 1 0.69 2 2 0.99 3 3 1.00 4 4 1.00 5 5 0.50 which is the same of na.rm=TRUE. On the contrary, in the following cases: aggregate(Value~ID, dta, max, na.action = na.pass) ID Value 1 1 0.69 2 2 0.99 3 3 1.00 4 4NA 5 5 0.50 aggregate(Value~ID, dta, max, na.action = na.fail) Error in na.fail.default(list(Value = c(0.69, 0.31, 0.01, 0.99, 1, NA the result is different. -- Best, GG [[alternative HTML version deleted]] __ R-help@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.
Re: [R] 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: > dta <- read.table(text="IDValue + 10.69 + 10.31 + 20.01 + 20.99 + 31.00 + 4NA + 40 + 41 + 50.5 + 50.5 + ", header=TRUE) > dput(dta) 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: > aggregate(Value~ID, dta, max, na.rm=TRUE) 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-boun...@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: > require(dplyr) > x %>% group_by(ID) %>% summarise(maxVal = max(Value,na.rm=TRUE)) On Fri, 2016-04-22 at 13:51 +, Saba Sehrish via R-help wrote: > Hi > > > I have two columns in data frame. First column is based on "ID" assigned to > each group of my data (similar ID depicts one group). From second column, I > want to identify highest value among each group and want to assign the same > ID to that highest value. > > Right now the data looks like: > > IDValue > 10.69 > 10.31 > 20.01 > 20.99 > 31.00 > 4NA > 40 > 41 > 50.5 > 50.5 > > I want to use R program to get results as below: > > ID Value > 10.69 > 20.99 > 31.00 > 41 > 50.5 > > Kindly guide me in this regard. > > Thanks > Saba > > __ > R-help@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. __ R-help@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. __ R-help@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.
Re: [R] Finding Highest value in groups
idvalues <- data.frame (ID = c(1, 1, 2, 2, 3, 4, 4, 4, 5, 5), Value = c(0.69, 0.31, 0.01, 0.99, 1.00, NA, 0,1, 0.5, 0.5)) aggregate(Value~ID, data=idvalues, max) ID Value 1 1 0.69 2 2 0.99 3 3 1.00 4 4 1.00 5 5 0.50 -- Best, GG [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Finding Highest value in groups
Assuming your dataframe is in a variable x: > require(dplyr) > x %>% group_by(ID) %>% summarise(maxVal = max(Value,na.rm=TRUE)) On Fri, 2016-04-22 at 13:51 +, Saba Sehrish via R-help wrote: > Hi > > > I have two columns in data frame. First column is based on "ID" assigned to > each group of my data (similar ID depicts one group). From second column, I > want to identify highest value among each group and want to assign the same > ID to that highest value. > > Right now the data looks like: > > IDValue > 10.69 > 10.31 > 20.01 > 20.99 > 31.00 > 4NA > 40 > 41 > 50.5 > 50.5 > > I want to use R program to get results as below: > > ID Value > 10.69 > 20.99 > 31.00 > 41 > 50.5 > > Kindly guide me in this regard. > > Thanks > Saba > > __ > R-help@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. __ R-help@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.