Portal Data Manipulation Pipes (solution)

NoteExercise

If the file surveys.csv is not already in your working directory then download a copy.

Load the file into R using read_csv().

Use pipes (|>) to combine the following operations to manipulate the data.

  1. Use select() and arrange() to create a new data frame with just the year, month, day, species_id, and plot_id columns with the rows sorted by species_id.
  2. Use mutate(), select(), and drop_na() to create a new data frame with the year, species_id, and hindfoot_length in cm of each individual, with no null hindfoot lengths. The hindfoot length in the table is given in mm so you will need to create a new column for hindfoot length in cm by dividing the hindfoot_length column by 10.
  3. Use filter() and select() to get the year, month, day, and species_id columns for all of the rows in the data frame where species_id is "OT".
  4. Use select() and filter() to produce a data frame with plot_id, species_id, weight, and hindfoot_length, where the species is "DM" or "DS" and hindfoot_length is greater than 35. Exclude NA values for both weight and hindfoot_length. Sort the result by hindfoot_length.
CautionOutput 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: 35549 Columns: 9
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): species_id, sex
dbl (7): record_id, month, day, year, plot_id, hindfoot_length, weight

ℹ 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: 35,549 × 5
    year month   day species_id plot_id
   <dbl> <dbl> <dbl> <chr>        <dbl>
 1  1980     7    21 AB               8
 2  1980     7    21 AB              24
 3  1980     7    21 AB              19
 4  1980     7    21 AB              22
 5  1980    12    15 AB              16
 6  1981     1    11 AB              22
 7  1981     1    11 AB              22
 8  1981     1    11 AB              20
 9  1981     1    12 AB               6
10  1981     6     4 AB              23
# ℹ 35,539 more rows
# A tibble: 31,438 × 3
    year species_id hindfoot_length_cm
   <dbl> <chr>                   <dbl>
 1  1977 NL                        3.2
 2  1977 NL                        3.3
 3  1977 DM                        3.7
 4  1977 DM                        3.6
 5  1977 DM                        3.5
 6  1977 PF                        1.4
 7  1977 DM                        3.7
 8  1977 DM                        3.4
 9  1977 PF                        2  
10  1977 DS                        5.3
# ℹ 31,428 more rows
# A tibble: 2,249 × 4
    year month   day species_id
   <dbl> <dbl> <dbl> <chr>     
 1  1977     7    17 OT        
 2  1977    10    16 OT        
 3  1977    10    17 OT        
 4  1977    10    17 OT        
 5  1977    11    12 OT        
 6  1977    12    10 OT        
 7  1977    12    10 OT        
 8  1977    12    10 OT        
 9  1977    12    10 OT        
10  1977    12    11 OT        
# ℹ 2,239 more rows
# A tibble: 8,718 × 4
   plot_id species_id weight hindfoot_length
     <dbl> <chr>       <dbl>           <dbl>
 1       7 DM             35              36
 2      18 DM             43              36
 3      12 DM             41              36
 4      18 DM             41              36
 5      13 DM             40              36
 6      20 DM             37              36
 7      22 DM             48              36
 8      22 DM             36              36
 9       6 DM             42              36
10      12 DM             44              36
# ℹ 8,708 more rows