Penguins data aggregation (solution)

NoteExercise

If the file penguins_no_na.csv is not already in your working directory then download it into your working directory.

Load penguins_no_na.csv into R using read_csv().

  1. Use the group_by() and summarize() functions to get a count of the number of individuals in each species.
  2. Use the group_by() and summarize() functions to calculate the average bill_length_mm for each species.
  3. Use the group_by() and summarize() functions to calculate the maximum flipper_length_mm for each species on each island.
  4. Use the filter(), group_by() and summarize() functions to get the mean body mass of Chinstrap penguins on each island.
  5. Find the mean bill ratio (bill length / bill depth) for each species on each island.
CautionOutput solution

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
Rows: 333 Columns: 8
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (3): species, island, sex
dbl (5): bill_length_mm, bill_depth_mm, flipper_length_mm, body_mass_g, year

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
  1. Use the group_by() and summarize() functions to get a count of the number of individuals in each species.
# A tibble: 3 × 2
  species   count
  <chr>     <int>
1 Adelie      146
2 Chinstrap    68
3 Gentoo      119
  1. Use the group_by() and summarize() functions to calculate the average bill_length_mm for each species.
# A tibble: 3 × 2
  species   avg_bill_length
  <chr>               <dbl>
1 Adelie               38.8
2 Chinstrap            48.8
3 Gentoo               47.6
  1. Use the group_by() and summarize() functions to calculate the maximum flipper_length_mm for each species on each island.
`summarise()` has regrouped the output.
ℹ Summaries were computed grouped by species and island.
ℹ Output is grouped by species.
ℹ Use `summarise(.groups = "drop_last")` to silence this message.
ℹ Use `summarise(.by = c(species, island))` for per-operation grouping
  (`?dplyr::dplyr_by`) instead.
# A tibble: 5 × 3
# Groups:   species [3]
  species   island    max_flipper_length
  <chr>     <chr>                  <dbl>
1 Adelie    Biscoe                   203
2 Adelie    Dream                    208
3 Adelie    Torgersen                210
4 Chinstrap Dream                    212
5 Gentoo    Biscoe                   231
  1. Use the filter(), group_by() and summarize() functions to get the mean body mass of Chinstrap penguins on each island.
# A tibble: 1 × 2
  island avg_body_mass
  <chr>          <dbl>
1 Dream          3733.
  1. Find the mean bill ratio (bill length / bill depth) for each species on each island.
`summarise()` has regrouped the output.
ℹ Summaries were computed grouped by species and island.
ℹ Output is grouped by species.
ℹ Use `summarise(.groups = "drop_last")` to silence this message.
ℹ Use `summarise(.by = c(species, island))` for per-operation grouping
  (`?dplyr::dplyr_by`) instead.
# A tibble: 5 × 3
# Groups:   species [3]
  species   island    avg_bill_ratio
  <chr>     <chr>              <dbl>
1 Adelie    Biscoe              2.13
2 Adelie    Dream               2.12
3 Adelie    Torgersen           2.12
4 Chinstrap Dream               2.65
5 Gentoo    Biscoe              3.18