Thank you A.K. and Ista. The answer that eliminates duplicated rows is A.K.'s first solution (brilliant idea using which.max!). The version of Ista's solution without duplicates is ddply(data, "state", function(x) x[which.max(x$date), ])$value Thanks again! Dan
On Thu, Feb 13, 2014 at 8:36 AM, arun <smartpink...@yahoo.com> wrote: > Hi, > Try ?which.max() # unique values for the combination. > > ddply(data,.(state),summarize,max_date=value[which.max(date)])[,2] > #or > ddply(data,.(state),summarize,max_date=value[date == max(date)])[,2] > > > A.K. > > > On Thursday, February 13, 2014 11:15 AM, Dan Murphy <chiefmur...@gmail.com> > wrote: > I can do this in multiple steps with summarise, joins, etc., but can't > help thinking it can be accomplished in one plyr call. > > Here's a small example: > >> require(plyr) >> require(lubridate) >> data <- data.frame( > + date = rep(as.Date(ymd(20140101 + (0:3) * 100)), 2), > + state = rep(c("A", "B"), each = 4), > + value = rnorm(8)) > > What I want is clearly > >> data[c(4, 8), "value"] > [1] -1.007111 -1.527541 > > Here are my multiple steps: > >> statemaxval <- ddply(data, "state", summarise, max_date = max(date)) >> rslt <- join(data, statemaxval, by = "state") >> rslt <- subset(rslt, date == max_date) >> rslt <- rslt[!duplicated(rslt), ] >> rslt$value > [1] -1.007111 -1.527541 > > Is there a one-step way to accomplish this? > Something like > ddply(data, "state", summarise, > "GiveMeTheValueCorrespondingToMaxDateByState!!") > > Or is that only possible if there is only one unique value for a given > combination of state and max(date)? > > Thanks, > Dan > > ______________________________________________ > R-help@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@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.