I would most likely be exploring the data.table package, although without any details, the following data.table solution will most likely not be what you need. I mention this because, in particular, there can be more than one βRatingβ entry for each group that corresponds to max ; how would you like to deal with these cases?
library(data.table) temp <- read.table(header = TRUE, stringsAsFactors=FALSE, text = "Website Datetime Rating A 2012-10-9 10 A 2012-11-10 12 B 2011-10-9 5") DT <- data.table(temp, key="Website") DT
I would recommend that for better answers you might want to include information such as how your datetime variable can affect your aggregation, or it is possible that there can be more than one "max" value for each group.
If you want all the lines that match max, the fix is ββeasy:
DT[, list(Time = Times[Rating == max(Rating)], Rating = max(Rating)), by = key(DT)]
If you need a Rating column, there are many ways around this. Following the same steps as above to convert to data.table , try:
DT[, list(Datetime = max(Rating)), by = key(DT)] Website Datetime
Or, keeping the original βpaceβ of data.frame , try aggregate() :
aggregate(Rating ~ Website, temp, max) Website Rating
Another approach using ave :
temp[with(temp, Rating == ave(Rating, Website, FUN=max)), ]