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

Do not use pipes for this exercise.

  1. Use select() to create a new data frame with just the year, month, day, and species_id columns in that order.
  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 the filter() function to get all of the rows in the data frame for the species ID SH.
Output 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 species_id
   <dbl> <dbl> <dbl> <chr>     
 1  1977     7    16 NL        
 2  1977     7    16 NL        
 3  1977     7    16 DM        
 4  1977     7    16 DM        
 5  1977     7    16 DM        
 6  1977     7    16 PF        
 7  1977     7    16 PE        
 8  1977     7    16 DM        
 9  1977     7    16 DM        
10  1977     7    16 PF        
# ℹ 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 × 9
   record_id month   day  year plot_id species_id sex   hindfoot_length weight
       <dbl> <dbl> <dbl> <dbl>   <dbl> <chr>      <chr>           <dbl>  <dbl>
 1        24     7    17  1977      13 SH         M                  21     NA
 2      1435    11     4  1978      17 SH         M                  29     89
 3      5847     5    21  1982      12 SH         M                  28    106
 4      6080     6    29  1982      12 SH         F                  NA     52
 5      7506     3    14  1983      21 SH         <NA>               NA     NA
 6      7681     4    16  1983      21 SH         <NA>               NA     NA
 7     11896    10     4  1986      21 SH         M                  28     55
 8     13156     7    26  1987      11 SH         M                  NA     77
 9     13255     8    26  1987       3 SH         F                  30     78
10     13491    10    24  1987      17 SH         F                  31    104
# ℹ 137 more rows