How to keep only the last or recent observation for each group?

You can group the data first with ‘group_by’ command, then use either ‘max’ or ‘last’ function in the ‘filter’ command.

It would look something like this.

in dplyr, it would look something like this.

data %>%
   group_by(CARRIER) %>%
   filter(date == max(date))