data wrangling to summarise and concatenate a column values of df

Please find the reprex/starting code below:
Basically I am trying to obtain the following output

# 2 columns containing cyl and combi
expected_output = 
cyl                                                                  combi
4                                                      Datsun 710 _ 22.8
6 Mazda RX4 _ 21,Mazda RX4 Wag _ 21,Hornet 4 Drive _ 21.4,Valiant _ 18.1
8                                               Hornet Sportabout _ 18.7

Steps following as below:

library(tidyverse)

# To convert index into columns
df <- mtcars %>% 
      head() %>%
      tibble::rownames_to_column("cars")

# concatenate columns
df["combi"] <- paste(df$cars,"_", df$mpg) 

#Struck from here as it involves concatenating strings of a column and summarising them vy concatenating

## Attempts to resolve 
df %>%
  group_by(cyl) %>%
  summarise()

df_ <- df %>% 
  pivot_wider(names_from = cyl, values_from = combi) %>% 
  mutate(mycol = coalesce(`6`,`4`,`8`)) 

x = df_ %>% 
select(`6`) %>% stringr::str_c(sep = "_")

assertr::col_concat(df, sep = "")

This should suffice:

df %>%
  group_by(cyl) %>%
  summarise(combi = paste(combi, collapse = ","))
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.