str_flatten()

  • str_flatten() = collapse a single character vector into a single string
  • Similar to base::paste(collapse = X)
  • Useful with summarize()
tribble(
  ~ name, ~ fruit,
  "Carmen", "banana",
  "Carmen", "apple",
  "Marvin", "nectarine",
  "Terence", "cantaloupe",
  "Terence", "papaya",
  "Terence", "madarine"
) |>
  group_by(name) |> 
  summarize(fruits = str_flatten(fruit, ", ", last = ", and "))
## # A tibble: 3 × 2
##   name    fruits                          
##   <chr>   <chr>                           
## 1 Carmen  banana, and apple               
## 2 Marvin  nectarine                       
## 3 Terence cantaloupe, papaya, and madarine