Dplyr shrub volume aggregation (solution)

Exercise

Dr. Morales 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. Load the data using read_csv.

  1. Use drop_na, group_by and summarize to calculate and print the average height of a plant in each experiment, while ignoring plants that have a height of NA when calculating the average.
  2. Use drop_na, group_by and summarize to determine the maximum height of a plant at each site, while ignoring plants that have a height of NA when calculating the average.
  3. Use mutate, drop_na, group_by and summarize to determine the average volume (volume = length * width * height) of a plant in each site, while ignoring plants that have volumes of NA when calculating the average.
  4. Use mutate, drop_na, group_by and summarize to determine both the average volume and the number of plants in each experiment, while ignoring plants that have volumes of NA when calculating the average and when counting plants.
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. Use drop_na(), group_by and summarize to calculate and print the average height of a plant in each experiment, while ignoring plants that have a height of NA when calculating the average.
# A tibble: 3 × 2
  experiment avg_height
       <dbl>      <dbl>
1          1       4.7 
2          2       5.12
3          3       3.85
  1. Use drop_na, group_by and summarize to determine the maximum height of a plant at each site, while ignoring plants that have a height of NA when calculating the average.
# 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
  1. Use mutate, drop_na, group_by and summarize to determine the average volume (volume = length * width * height) of a plant in each site, while ignoring plants that have volumes of NA when calculating the average.
# A tibble: 4 × 2
   site avg_volume
  <dbl>      <dbl>
1     1       23.8
2     2       26.6
3     3       23.1
4     4       57.1
  1. Use mutate, drop_na, group_by and summarize to determine both the average volume and the number of plants in each experiment, while ignoring plants that have volumes of NA when calculating the average and when counting plants.
# A tibble: 3 × 3
  experiment avg_volume num_plants
       <dbl>      <dbl>      <int>
1          1       22.0          4
2          2       53.8          4
3          3       22.1          4