Portal Data Manipulation Pipes (solution)

Exercise

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 (either |> or %>%) to combine the following operations to manipulate the data.

  1. Use mutate(), select(), and drop_na() to create a new data frame with the year, species_id, and weight in kilograms of each individual, with no null weights.
  2. Use the 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 SH.
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: 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: 32,283 × 3
    year species_id wgt_kg
   <dbl> <chr>       <dbl>
 1  1977 DM          0.04 
 2  1977 DM          0.048
 3  1977 DM          0.029
 4  1977 DM          0.046
 5  1977 DM          0.036
 6  1977 DO          0.052
 7  1977 PF          0.008
 8  1977 OX          0.022
 9  1977 DM          0.035
10  1977 PF          0.007
# ℹ 32,273 more rows
# A tibble: 147 × 4
    year month   day species_id
   <dbl> <dbl> <dbl> <chr>     
 1  1977     7    17 SH        
 2  1978    11     4 SH        
 3  1982     5    21 SH        
 4  1982     6    29 SH        
 5  1983     3    14 SH        
 6  1983     4    16 SH        
 7  1986    10     4 SH        
 8  1987     7    26 SH        
 9  1987     8    26 SH        
10  1987    10    24 SH        
# ℹ 137 more rows