0

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?

Silent_bliss
  • 307
  • 1
  • 6

1 Answers1

1

(EDIT after comment)

One possible solution would be

df %>% 
  group_by(year) %>% 
  summarise(bus = paste(bus, collapse = ",")) 

Output

# A tibble: 2 x 2
# Groups:   year [2]
#    year bus    
#   <dbl> <chr>  
# 1  1978 29C,12B
# 2  1979 25G,27C
Ric S
  • 9,073
  • 3
  • 25
  • 51
  • Why not `summarise()` instead of `mutate() %>% distinct()`? – Sotos Jun 30 '20 at 13:36
  • It was the first solution that came to mind, but I agree that `summarise` is more efficient (and uses one less statement). Thank you for pointing that out! – Ric S Jun 30 '20 at 13:39