Code Studio: Filter and Summarise Cars (dplyr)
Practise the tibble and dplyr style from The Tibble Format on the built-in mtcars data.
The program reads a number of cylinders (4, 6 or 8). Using the pipe |>:
- Turn
mtcarsinto a tibble withas_tibble(). filter()it to cars with that many cylinders.summarise()three values: the number of cars (n()), the meanmpgrounded to 1 decimal place, and the maximumhp.
Print the result as:
Cars: 11 Mean mpg: 26.7 Max hp: 113
Try printing the tibble itself with print() before you summarise. How does it look different from a plain data frame?
Practise the tibble and dplyr style from The Tibble Format on the built-in mtcars data.
The program reads a number of cylinders (4, 6 or 8). Using the pipe |>:
- Turn
mtcarsinto a tibble withas_tibble(). filter()it to cars with that many cylinders.summarise()three values: the number of cars (n()), the meanmpgrounded to 1 decimal place, and the maximumhp.
Print the result as:
Cars: 11 Mean mpg: 26.7 Max hp: 113
Try printing the tibble itself with print() before you summarise. How does it look different from a plain data frame?