Part 1. Checking data types

Setup

  • Create a new repo on GitHub for today’s activities
  • Clone to create a version controlled R Project
  • Create subfolders called docs, data, and figs
  • Create a Quarto document, save in the docs subfolder as r_data_types.qmd

Create some data, check the classes, index!

Vectors, lists & data frames

In your Quarto document:

  1. Create a vector called vec_1containing the following:
2, 5, 9, 10, 8, 12, 1, 0

Check the following for that vector:

  • What is the class of the vector? class()
  • What type of variable does it store? typeof()
  • Access the 3rd element and store as vec_1_e3
  • Access the 1st element and store as vec_1_e1
  • Access the 5th through 7th elements and store as vec_1_e5to7
  • Reassign vec_1 as a character using as.character, stored as vec_1_char. What does the output look like?
  1. Create a vector called vec_2

vec_2 should contained named elements, where town = "Santa Barbara, location = "Rincon", swell = "south"

  • Take a look at what you’ve made
  • What is the class of vector elements? class()
  • What is the length of vec_2?
  • Access the 2nd element by name and store as vec_2_e2
  1. Create a data frame in R, index

Write code to create a data frame called df_1 that looks like this:

##   region     species count
## 1      A       otter    12
## 2      B great white     2
## 3      A    sea lion    36
## 4      D  gray whale     6
  • Return the class of the entire data frame
  • Return the class of the species column
  • Find the maximum value of the count() column, store as max_count

Part 2. Wild data

Set-up

  • Visit the EDI site to learn about Mack Creek salamander & cutthroat trout data you’ll be using here: data package

  • Download the first CSV listed (AS00601.csv), and take a look at it (outside of R is fine as a first step, e.g. you can open the CSV in Excel)

  • Explore the metadata (see View Full Metadata in the Resources section of the data website)

  • What does each column contain? What are the units of each? What is the study overall about?

  • Create a new Quarto document and save it in your docs folder. Attach the tidyverse, here and janitor packages in the setup chunk (you choose the file name)

  • Set global options in the YAML so that messages and warnings do NOT show up in the rendered document

  • Save the AS00601.csv in your data folder of your project

Read in the data

  • Read in the data using read_csv() with here(), store as mack_verts

  • Look at what you’ve read in (e.g. with view())

A bit of wrangling & exploring

  • Update the variable names in mack_verts to lower snake case

  • In a new code chunk, practice accessing individual pieces of the data frame (there is no real functionality to this right now, but just to reinforce stuff we learned in our interactive session):

    • Store the 5th value in column “WEIGHT” as mc_wt_5. Check by looking at your data frame to confirm.
    • Store the 8th - 20th value in the “LENGTH1” column as mc_length_8_20. Check by looking at your data frame to confirm.
    • Store everything in column SAMPLEDATE as a vector called mc_dates

Make a salamander subset

  • Create a subset that only contains observations for Pacific Giant Salamanders (species Dicamptodon tenebrosus, stored in species as DITE). Store the subset as mc_salamanders. Hint: see dplyr::filter()!

Make a scatterplot of salamander length x weight

  • Create a scatterplot of length1 (snout-vent length in millimeters) versus weight (grams) for all salamanders in the subset you created above, mc_salamanders. Update axis labels, title, subtitle, and add a caption with the data source. Customize point color and size, possibly opacity, and theme.

  • Export your scatterplot as salamander_size.png to your figs folder.

Make a cutthroat plot

  • Similar to above, make a subset called mc_trout that only contains observations for cutthroat trout (species “ONCL”)
  • Create a scatterplot of length1 by weight for all trout in the dataset
  • Customize so that the point color depends on reach
  • Customize your color scheme (e.g. scale_color_manual())
  • Facet your plot by creek reach (facet_wrap(~...))
  • Update graph axis labels and title
  • Export your graph as cutthroat_size.png to the figs folder

Stage, commit, pull, push

  • Make sure your changes are safely stored by pushing to GitHub
  • Close your project locally
  • Reopen your project locally
  • Reopen your .qmd files for the activities you did today.
  • Render. Does it work? Done.

End Day 2 activities