0

I am struggling with the tidyverse package. I'm using the mpg dataset from R to display the issue that I'm facing (ignore if the relationships are not relevant, it is just for the sake of explaining my problem).

What I'm trying to do is to obtain the average "displ" grouped by manufacturer and year AND at the same time (and this is what I can't figure out), have several columns for each of the fuel types variable (i.e.: a column for the mean of diesel, a column for the mean of petrol, etc.).

This is the first part of the code and I'm new to R so I really don't know what do I need to add...

mpg %>%
  group_by(manufacturer, year) %>%
  summarize(Mean. = mean(c(displ)))

# A tibble: 30 × 3
# Groups:   manufacturer [15]
   manufacturer  year Mean.
   <chr>        <int> <dbl>
 1 audi          1999  2.36
 2 audi          2008  2.73
 3 chevrolet     1999  4.97
 4 chevrolet     2008  5.12
 5 dodge         1999  4.32
 6 dodge         2008  4.42
 7 ford          1999  4.45
 8 ford          2008  4.66
 9 honda         1999  1.6 
10 honda         2008  1.85
# … with 20 more rows

Any help is appreciated, thank you.

2
  • I don't want to take means for different variables but instead for one variable (displ) divide it into the several values of the fl variable. I hope I explained myself a bit better. Commented Sep 10, 2021 at 17:31
  • Can you update your post with the expected output that you are looking for the mpg dataset? Commented Sep 11, 2021 at 5:16

1 Answer 1

2

Perhaps, we need to reshape into 'wide'

library(dplyr)
library(tidyr)
mpg %>% 
   select(manufacturer, year, fl, displ) %>%
   pivot_wider(names_from = fl, values_from = displ, values_fn = mean)

-output

# A tibble: 30 x 7
   manufacturer  year     p     r     e     d     c
   <chr>        <int> <dbl> <dbl> <dbl> <dbl> <dbl>
 1 audi          1999  2.36 NA    NA     NA    NA  
 2 audi          2008  2.73 NA    NA     NA    NA  
 3 chevrolet     2008  6.47  4.49  5.3   NA    NA  
 4 chevrolet     1999  5.7   4.22 NA      6.5  NA  
 5 dodge         1999 NA     4.32 NA     NA    NA  
 6 dodge         2008 NA     4.42  4.42  NA    NA  
 7 ford          1999 NA     4.45 NA     NA    NA  
 8 ford          2008  5.4   4.58 NA     NA    NA  
 9 honda         1999  1.6   1.6  NA     NA    NA  
10 honda         2008  2     1.8  NA     NA     1.8
# … with 20 more rows
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.