Dplyr shrub volume aggregation (solution)

Exercise

Dr. Morales has conducted an experiment looking at the effect of three different treatments on shrub volume at four different sites. She wants some summary data of the plants at her sites and for her experiments. If the file shrub-volume-data.csv is not already in your work space download it.

This code calculates the average height of a plant at each site:

shrub_dimensions <- read_csv('shrub-volume-data.csv')
shrub_dimensions_no_na <- drop_na(shrub_dimensions, height)
by_site <- group_by(shrub_dimensions, site)
avg_height <- summarize(by_site, avg_height = mean(height))
avg_height
  1. Copy the above code and modify it to calculate and print the average height of a plant in each experiment.
  2. Copy the above code and modify it to use max() to determine the maximum height of a plant at each site.
Output 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: 15 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (5): site, experiment, length, width, height

ℹ 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. Copy the above code and modify it to calculate and print the average height of a plant in each experiment.
Rows: 15 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (5): site, experiment, length, width, height

ℹ 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.
# A tibble: 3 × 2
  experiment avg_height
       <dbl>      <dbl>
1          1       4.7 
2          2       5.12
3          3       3.85
  1. Copy the above code and modify it to use max() to determine the maximum height of a plant at each site.
Rows: 15 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (5): site, experiment, length, width, height

ℹ 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.
# A tibble: 5 × 2
   site max_height
  <dbl>      <dbl>
1     1        9.6
2     2        4  
3     3        7.5
4     4        6.5
5     5        5.2