Skip to content
Prev 58764 / 398502 Next

Conditional selection of rows

Many thanks to Gabor Grothendieck, Thomas Lumley and James Holtman for their 
useful answers on this thread.  The three solutions worked for the problem.  
Here is a sumary of their responses (modified for consistency on notations):
Gabor Grothendieck's solution:


subset<-do.call("rbind", by(data, data$Iteration, tail, 1))


James Holtman's solution:

subset<- by(data, data$Iteration, function(x)x[nrow(x),])
subset<-do.call('rbind',subset)

Thomas Lumley's solution:

data <- data[order(data$Iteration, data$Day, decreasing=TRUE),]

subset <- data[!duplicated(data$Iteration),]

If you are sure that the data are in order to begin with you could just 
reverse the entire data set (  data <- data[nrow(data):1,] ), but I'm always 
reluctant to assume this.