I have data with repeated observations that sometimes match on two elements but differ on a third, and sometimes match only on the first. For example:
name <- c("John", "Mary", "Anna", "Anna", "John", "Mary", "Anna", "John")
sport <- c("soccer", "basketball", "tennis", "tennis", "soccer", "soccer", "badminton", "basketball")
time <- c(41, 5, 10, 61, 1, 12, 18, 99)
data <- cbind(name, sport, time)
name sport time
John soccer 41
Mary basketball 5
Anna tennis 10
Anna tennis 61
John soccer 1
Mary soccer 12
Anna badminton 18
John basketball 99
For each observation that matches on the first two columns (e.g. here, on both name and sport), I want to keep only the observation with the greatest time value. For those that match only on the first column (e.g. name), I want to keep them as is. For example:
name sport time
John soccer 41
Mary basketball 5
Anna tennis 61
Mary soccer 12
Anna badminton 18
John basketball 99
How would I do this?