I have a data frame that looks like this.
df <- data.frame(year = c(1978, 1978, 1979, 1979), bus = c("29C", "12B", "25G", "27C"))
I wish to create unique values for year column and merge the values of the bus column. Expected output is as follows
# year bus
# 1978 29C,12B
# 1979 25G,27C
The code I have tried and the output I have got
library(dplyr)
df %>% group_by(year) %>% distinct(year) %>% unite(bus, sep = ",", na.rm = F)
# A tibble: 2 x 1
# bus
# <chr>
#1 1978
#2 1979
What is the mistake in the code and how can I achieve the expected output?