Dear Andrew: Here is another way assuming you have an order column in your history data as well as a person_id. Again, your variable of interest is x: # Some data set.seed(1) history<-data.frame( person_id=rep(1:10,each=10), record=rep(sample(10),10), x=rnorm(100) )
history # Splitting the data by person_id spl<-with(history,split(mydata,person_id)) # splitting by person_id spl # Selecting the last record of x by person_d and reporting # the person_id and the x value -- you can change this to your convenience :) do.call(rbind,lapply(spl,function(x) x[which.max(x[,2]),c(1,3)])) 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.