Dplyr shrub volume join (solution)

Exercise

Dr. Morales has conducted an experiment looking at the effect of three different treatments on shrub volume at four different sites. In addition to the main data table on shrub dimensions, Dr. Morales has two additional data tables. The first describes the manipulation for each experiment. The second provides information about the different sites.

If the files shrub-volume-data.csv, shrub-volume-experiments.csv, and shrub-volume-sites.csv aren’t in your work space download them. Import all three files.

  1. Use inner_join to combine the experiments data with the shrub dimensions data (from shrub-volume-data.csv) to add a manipulation column to the shrub dimensions data.
  2. Combine the sites data with both the shrub dimensions data and the experiments data to produce a single data frame that contains all of the data.
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.
Rows: 3 Columns: 2
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): manipulation
dbl (1): experiment

ℹ 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.
Rows: 4 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (4): site, latitude, longitude, elevation

ℹ 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. Import the experiments data and then use inner_join to combine

# A tibble: 15 × 6
    site experiment length width height manipulation
   <dbl>      <dbl>  <dbl> <dbl>  <dbl> <chr>       
 1     1          1    2.2   1.3    9.6 control     
 2     1          2    2.1   2.2    7.6 burn        
 3     1          3    2.7   1.5    2.2 rainout     
 4     2          1    3     4.5    1.5 control     
 5     2          2    3.1   3.1    4   burn        
 6     2          3    2.5   2.8    3   rainout     
 7     3          1    1.9   1.8    4.5 control     
 8     3          2    1.1   0.5    2.3 burn        
 9     3          3    3.5   2      7.5 rainout     
10     4          1    2.9   2.7    3.2 control     
11     4          2    4.5   4.8    6.5 burn        
12     4          3    1.2   1.8    2.7 rainout     
13     5          1    2.6   0.8   NA   control     
14     5          2    1.8  NA      5.2 burn        
15     5          3    3.1   2.2   NA   rainout     

2. Import the sites data and the combine it with both the data on shrub dimensions and the data on experiments to produce a single data frame that contains all of the data.

# A tibble: 12 × 9
    site experiment length width height manipulation latitude longitude
   <dbl>      <dbl>  <dbl> <dbl>  <dbl> <chr>           <dbl>     <dbl>
 1     1          1    2.2   1.3    9.6 control          29.6     -82.3
 2     1          2    2.1   2.2    7.6 burn             29.6     -82.3
 3     1          3    2.7   1.5    2.2 rainout          29.6     -82.3
 4     2          1    3     4.5    1.5 control          29.3     -82.4
 5     2          2    3.1   3.1    4   burn             29.3     -82.4
 6     2          3    2.5   2.8    3   rainout          29.3     -82.4
 7     3          1    1.9   1.8    4.5 control          29.8     -82.2
 8     3          2    1.1   0.5    2.3 burn             29.8     -82.2
 9     3          3    3.5   2      7.5 rainout          29.8     -82.2
10     4          1    2.9   2.7    3.2 control          30.0     -82.6
11     4          2    4.5   4.8    6.5 burn             30.0     -82.6
12     4          3    1.2   1.8    2.7 rainout          30.0     -82.6
# ℹ 1 more variable: elevation <dbl>