These exercises cover the sections of Data wrangling with tidy.

All files can be found in the “dataset” directory.

 

Exercise 3

 

  1. Group df1 by the variable age_class.
  2. Filter to get the biggest 5 by the variable length in each group.
  3. Summarise this data frame over the variable length by calculating the mean.

ANSWERS

Answer for Q1

## # A tibble: 97 x 5
## # Groups:   age_classbylength [4]
##    salmon_id common_name    age_classbylength  length_mm IGF1_ng_ml
##        <dbl> <chr>          <chr>                  <dbl>      <dbl>
##  1     35032 Chinook salmon yearling                 147       41.3
##  2     35035 Sockeye salmon juvenile                 121       NA  
##  3     35036 Sockeye salmon juvenile                 112       NA  
##  4     35037 Steelhead      juvenile                 220       42.7
##  5     35038 Steelhead      juvenile                 152       NA  
##  6     35033 Chinook salmon mixed age juvenile       444       62.1
##  7     35034 Sockeye salmon juvenile                 139       NA  
##  8     35048 Steelhead      juvenile                 288       24.2
##  9     35049 Steelhead      juvenile                 190       NA  
## 10     35050 Steelhead      juvenile                 283       63.5
## # … with 87 more rows

Answer for Q2

## # A tibble: 14 x 5
## # Groups:   age_classbylength [4]
##    salmon_id common_name    age_classbylength  length_mm IGF1_ng_ml
##        <dbl> <chr>          <chr>                  <dbl>      <dbl>
##  1     35033 Chinook salmon mixed age juvenile       444       62.1
##  2     35048 Steelhead      juvenile                 288       24.2
##  3     35050 Steelhead      juvenile                 283       63.5
##  4     35051 Steelhead      juvenile                 279       61.2
##  5     35060 Steelhead      juvenile                 248       40.2
##  6     35043 Steelhead      juvenile                 272       42.6
##  7     35143 Chinook salmon yearling                 204       80.9
##  8     35146 Chinook salmon mixed age juvenile       422      101. 
##  9     35095 Chinook salmon subyearling               90       NA  
## 10     35103 Chinook salmon yearling                 216       81.2
## 11     35110 Chinook salmon mixed age juvenile       275       81.5
## 12     35112 Chinook salmon yearling                 205       90.5
## 13     35115 Chinook salmon yearling                 215       53.5
## 14     35129 Chinook salmon yearling                 225       72.7

Answer for Q3

## # A tibble: 4 x 2
##   age_classbylength  mean_length_mm
##   <chr>                       <dbl>
## 1 juvenile                     274 
## 2 mixed age juvenile           380.
## 3 subyearling                   90 
## 4 yearling                     213