Portal Data Manipulation (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().

Do not use pipes for this exercise.

  1. Use select() and arrange() to create a new data frame with just the year, month, day, and plot_id columns with the rows sorted by plot_id.
  2. 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. The weight in the table is given in grams so you will need to create a new column for weight in kilograms by dividing the weight column by 1000.
  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 SH.
  4. Use select(), and filter() to produce a data frame with plot_id, species_id, weight, and hindfoot_length, where the species is "PB" or "PP" and weight is less than 40. Exclude NA values for both weight and hindfoot_length. Sort the table by weight.
CautionOutput solution

Code solution for Portal Data Manipulation


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 × 4
    year month   day plot_id
   <dbl> <dbl> <dbl>   <dbl>
 1  1977     7    16       1
 2  1977     7    16       1
 3  1977     7    16       1
 4  1977     8    19       1
 5  1977     8    19       1
 6  1977     9    13       1
 7  1977     9    13       1
 8  1977     9    13       1
 9  1977    10    16       1
10  1977    10    16       1
# ℹ 35,539 more rows
# 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
# A tibble: 5,395 × 4
   plot_id species_id weight hindfoot_length
     <dbl> <chr>       <dbl>           <dbl>
 1       4 PP              4              21
 2       5 PP              5              21
 3       1 PP              7              21
 4      21 PP              7              22
 5       4 PP              7              20
 6      11 PP              8              21
 7       8 PP              8              21
 8       4 PP              8              20
 9       8 PP              8              21
10       1 PP              8              20
# ℹ 5,385 more rows