Dplyr fix the code (solution)

Exercise

If shrub-volume-data.csv isn’t already in your working directory download it.

The following code is supposed to import the shrub volume data and calculate the mean shrub volume for each site and, separately, for each experiment (ignoring NA’s).

read_csv("shrub-volume-data.csv")

shrub_volume_by_site <- shrub_data |>
  mutate(volume = length * width * height) |>
  drop_na(volume) |>
  group_by(site) |>
  summarize(mean_volume = max(volume))
shrub_volume_by_site

shrub_volume_by_experiment <- shrub_data |>
  mutate(volume = length * width * height)
  group_by(site) |>
  summarize(mean_volume = mean(volume))
shrub_volume_by_experiment
  1. Fix the errors in the code so that it does what it’s supposed to
  2. Add one or more comments to the code explaining what it does
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.
# A tibble: 4 × 2
   site mean_volume
  <dbl>       <dbl>
1     1        23.8
2     2        26.6
3     3        23.1
4     4        57.1
# A tibble: 3 × 2
  experiment mean_volume
       <dbl>       <dbl>
1          1        22.0
2          2        53.8
3          3        22.1