'Aggregates types in R

I need to write a script in R (Rstudio) that produces a new table, with 6 rows, that aggregates the vehicle types in cars dataset. In the first field will be the type of vehicle (sports car, SUV, Wagon, Minivan, Pickup, regular), (regular, this is when everyone is zero (, and then there are the averages per value for each type of vehicle.

The table should look like this :

Type Wheel base Weight HWY MPG city MPG HP Cyl Engine size Dealer cost Retail price
Regular
Sport car
...

A photo of the file (cars dataset) is mentioned here : enter image description here



Solution 1:[1]

It would be much easier to answer this if you posted a reproducible example.

However this should work:

library(dplyr)
cars  %>% 
    group_by(Type)  %>% 
    mutate(across(`Wheel base`:`Retail price`, mean))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SamR