Dear Andrew, Here is one way: # Some data set.seed(1) mydata<-data.frame(person_id=rep(1:10,10),x=rnorm(100)) mydata
# last register for person_id with(mydata,tapply(x,person_id,function(x) tail(x,1))) # test for person_id=1 mydata[mydata$person_id==1,] # see the last number in column 2 HTH, Jorge On Fri, Feb 27, 2009 at 2:02 PM, Andrew Ziem <ahz...@gmail.com> wrote: > I want to find the last record for each person_id in a data frame > (from a SQL database) ordered by date. Is there a better way than > this for loop? > > for (i in 2:length(history[,1])) { > if (history[i, "person_id"] == history[i - 1, "person_id"]) > history[i, "order"] = history[i - 1, "order"] + 1 # same person > else > history[i, "order"] = 1 # new person > } > > # ignore all records except the last for each con_id > history2 <- subset(history, order == 1) > > > Andrew > > ______________________________________________ > 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. > [[alternative HTML version deleted]] ______________________________________________ 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.