[1] "Great White Shark"
EVR 628- Intro to Environmental Data Science
Rosenstiel School of Marine, Atmospheric & Earth Science and Institute for Data Science & Computing
By the end of this week, you should be able to:
pipe operatorHelping others (and future you) read your code
Is this comment helpful?
[1] "Great White Shark"
How about this?
# Build data
## Vector of shark lengths
lengths <- c(6, 4.1, 2.8, 5.5, 3.9, 5.8)
## Vector of shark names
sharks <- c("Great White Shark", "Lemon Shark",
"Bull Shark", "Hammerhead Shark",
"Mako Shark", "Great White Shark")
# Combine vectors into a data.frame
shark_data <- data.frame(
lengths,
sharks
)
# Find the length for the largest great white
shark_data$lengths[shark_data$sharks == "Great White Shark"] |> max()[1] 6
A script contains more than one bit of code, often dozens of lines
R can detect up to 6 levels, as indicated by the number of “#”
################################################################################
# Description goes here
################################################################################
# SET UP #######################################################################
## Load packages ---------------------------------------------------------------
## Load data -------------------------------------------------------------------
# PROCESSING ###################################################################
## Some step -------------------------------------------------------------------
# VISUALIZE ####################################################################
## Another step ----------------------------------------------------------------
# EXPORT #######################################################################
## The final step --------------------------------------------------------------
Use spaces:
^ (i.e. + , - , == , < , …)<- )|> and %>%)Not great
library(EVR628tools)
library(tidyverse)
data("data_lionfish")
my_data<-data_lionfish[data_lionfish$site=="Paamul",]
ggplot(data=my_data,
mapping=aes(x=depth_m,y=total_length_mm))+
geom_point(shape=21,fill="steelblue",size=2)+
labs(x="Depth(m)",
y="Totallength(mm)",
title="Bodylengthanddepth",
subtitle="Largerfishtendtolivedeeper",
caption="SourceEVR628tools::data_lionfish")Better
# Load packages
library(EVR628tools)
library(tidyverse)
# Load data
data("data_lionfish")
# Build my own data
my_data <- data_lionfish[data_lionfish$site == "Paamul",]
# Build my plot
ggplot(data = my_data,
mapping = aes(x = depth_m, y = total_length_mm)) +
geom_point(shape = 21, fill = "steelblue", size = 2) +
labs(x = "Depth (m)",
y = "Total length (mm)",
title = "Body length and depth",
subtitle = "Larger fish tend to live deeper",
caption = "Source EVR628tools::data_lionfish")Use Cmd + i to auto-indent
Things and types of things
The most common atomic classes are:
"a" or 'b' (note the quotation marks)2 or 10e3 (note scientific notation)TRUE/FALSE or T/F, but never true/false (note no quotations)You can check classes with function class()
Classes in R map to the types of data we saw during Week 3:
numericcharactercharacter called factor<-
Opt + -Alt + -x <- y” as: “x gets a value of y”They will appear in your environment pane

Arithmetic Operators:
numeric (or objects that can be coerced to them)
TRUE or FALSE (logical values) can be “coerced” to ones and zeroesnumericRelational operators
logical[1] FALSE
[1] TRUE
[1] FALSE
logicalYou can combine atomic elements with function “c()”
c stands for “combine”
colors and numbers are vectors of class character and numericVectors are like columns in a spreadsheet
Vectors have lengths: number of elements
If the vector is numeric, arithmetic operations are applied to every element automatically
[1] 1 2 3 4 5 6 7 8 9 10
[1] 2 4 6 8 10 12 14 16 18 20
[1] 6 7 8 9 10 11 12 13 14 15
[]Accessing elements within an object based on their position
Read “[]” as “extract elements”
[] and modify with <-[1] "red" "blue" "green" "orange" "black"
Let’s modify red to white
Using vectors of different lengths fails
Error in `data.frame()`:
! arguments imply differing number of rows: 3, 2
Unless they can be “recycled”
| colors | numbers |
|---|---|
| red | 4 |
| green | 2 |
| blue | 4 |
| orange | 2 |
Note how I built the data.frame directly from atomic elements
Build a data.frame and save it to an object
They allow us to adhere to the standards described in Week 2:
Tidy data
spp sex carapace_length
1 C. mydas Female 23
2 C. caretta Male 24
Not tidy data
variable Org 1 Org 2
1 spp C. mydas C. caretta
2 sex Female Male
3 carapace_length 23 24
ggplotTibbles are a special type of data.frame used in tidyverse and spatial libraries
They work in the same way
Tibbles are also smart
# A tibble: 109 × 9
id site lat lon total_length_mm total_weight_gr size_class depth_m
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <dbl>
1 001-Po-… Para… 20.5 -87.2 213 113. large 38.1
2 002-Po-… Para… 20.5 -87.2 124 27.6 medium 27.9
3 003-Pd-… Pared 20.5 -87.2 166 52.3 medium 18.5
4 004-Cs-… Cano… 20.5 -87.2 203 123. large 15.5
5 005-Cs-… Cano… 20.5 -87.2 212 129 large 15
6 006-Pl-… Paam… 20.5 -87.2 210 139. large 22.7
7 007-Pl-… Paam… 20.5 -87.2 132 50.3 medium 13.4
8 008-Po-… Para… 20.5 -87.2 122 17.2 medium 18.5
9 009-Po-… Para… 20.5 -87.2 224 113. large 18.2
10 010-Pd-… Pared 20.5 -87.2 117 19.6 medium 12.5
# ℹ 99 more rows
# ℹ 1 more variable: temperature_C <dbl>
[]data.frames are two-dimensional, so we use two numbers: [rows, cols]
[1] "A"
Extract values for first observation across all variables, implicitly
colors numbers letters
1 red 1 A
[1] "red" "green"
$$[] on the resulting vector [1] "001-Po-16/05/10" "002-Po-29/05/10" "003-Pd-29/05/10" "004-Cs-12/06/10"
[5] "005-Cs-12/06/10" "006-Pl-21/06/10" "007-Pl-21/06/10" "008-Po-04/07/10"
[9] "009-Po-04/07/10" "010-Pd-08/07/10"
[] and modify with <-Works in the same way as with vectors
A function:
Some functions we’ve used so far are:
length()dim()mean()aes()Imagine having the following numeric vector
And wanting to know how many square plots have a side greater than 2
Before 2013 you had two options:
1: Call each function at a time
The magrittr package introduced the pipe operator “%>%”


{dplyr}%>%” as “goes into”The R community saw the usefulness of this and developed a “native” pipe “|>”
You no longer need to load a package to use a pipe
[1] 5
Cmd + Shift + MCtrl + Shift + MFor example, calculate the mean \(\bar{x} = \frac{\sum_{i = 1}^Nx_i}{N} = \frac{x_1 + x_2 + x_3 ... x_N}{N}\)
“Sum of all values, divided by the number of values”
If you have to do this for multiple variables, you might want to use a function
Comments
Text embedded within your script for humans to read
Helps other people (including future you) understand what’s going on
We use the
#sign to prevent the computer from reading it