Использование dplyr:
library(dplyr)
df <- data.frame(user =c("P001", "P001", "P002"), tripID = c("tid1", "tid2", "tid3"), mode =c("bus", "train", "taxi"), Origin = c("Westmead", "Redfan", "Westmead"), Destination = c("Redfan", "Darlington", "Strathfield"), depart_dt = c("8:00", "8:30", "8:45") )
df %>%
group_by(user) %>%
arrange(depart_dt, .by_group = TRUE) %>%
summarize(Origin = Origin[1], mode = paste(mode, collapse = " + "),
Destination = Destination[length(Destination)],
depart_dt = depart_dt[1]) %>%
mutate(tripID = paste0("newtid", row_number()))
#> # A tibble: 2 x 6
#> user Origin mode Destination depart_dt tripID
#> <fct> <fct> <chr> <fct> <fct> <chr>
#> 1 P001 Westmead bus + train Darlington 8:00 newtid1
#> 2 P002 Westmead taxi Strathfield 8:45 newtid2
Создано 06.05.2020 с помощью пакета REPEX (v0.3.0)