Warning: package 'ggplot2' was built under R version 4.5.2
Warning: package 'tidyr' was built under R version 4.5.2
Warning: package 'dplyr' was built under R version 4.5.2
EVR 628- Intro to Environmental Data Science
Warning: package 'ggplot2' was built under R version 4.5.2
Warning: package 'tidyr' was built under R version 4.5.2
Warning: package 'dplyr' was built under R version 4.5.2
################################################################################
# title
################################################################################
#
# Your Name Here
# Your email here
# date
#
# Description
#
################################################################################objects_and_classes.RIn your R Script, create objects of different classes and check their types:
my_name with your namemy_age with your age (or any number, it doesn’t have to be your age)is_student with TRUE or FALSEclass()my_name <- "JC" # character object called `my_name` with your name
my_age <- 32 # numeric object called `my_age` with your age
is_student <- FALSE # logical object called `is_student` with TRUE or FALSE
# Check the class of each object using `class()`
class(my_name)[1] "character"
class(my_age)[1] "numeric"
class(is_student)[1] "logical"
Let’s see what happens when you try converting objects. In the R script, write and then execute code that will:
character using as.character(my_age)TRUE to numeric using as.numeric()"hello" to numeric - what happens?|> to build a code pipeline to:
# Note that I am overwriting the object. If I had only done `as.character(my_age)`,
# then the coerced output would have been printed to the console.
my_age <- as.character(my_age) # Convert your age to `character`
# Check that iw worked
class(my_age)[1] "character"
as.numeric(TRUE) # Convert `TRUE` to numeric using `as.numeric()`[1] 1
as.numeric("hello") # Try to convert `"hello"` to `numeric` - what happens?Warning: NAs introduced by coercion
[1] NA
# Build a pipeline
my_age <- 32 # Start with my_age as numeric again
# ANd now build the pipeline
my_age |>
as.character() |>
class()[1] "character"
length_m with values: 6, 4.1, 2.8, 5.5, 3.9, 5.8shark_species with: Great White Shark, Lemon Shark, Bull Shark, Hammerhead Shark, Mako Shark, and Great White Shark (yes, white shark again)length()|> to build a pipeline)mean()max()# Q2
length_m <- c(6, 4.1, 2.8, 5.5, 3.9, 5.8)
# Q3
shark_species <- c("Great White Shark", "Lemon Shark", "Bull Shark",
"Hammerhead Shark", "Mako Shark", "Great White Shark")
# Q5
length(length_m)[1] 6
# Q6
shark_species |>
unique() |>
length()[1] 5
# Q7
mean(x = length_m)[1] 4.683333
# Q8
max(length_m)[1] 6
= not <-<- not =[] and save them to an object called first_3length_m and sharks_species are ordered so that they match each other, find the shark species that is the largest# Q1
first_3 <- shark_species[1:3] # Extract values 1 through 3 and assign them
first_3 # See the values I assigned[1] "Great White Shark" "Lemon Shark" "Bull Shark"
# Q2
shark_species[length_m > 4] #Extract shark species where maximum length is greater than 4 meters[1] "Great White Shark" "Lemon Shark" "Hammerhead Shark"
[4] "Great White Shark"
# Q3
shark_species[length_m == max(length_m)][1] "Great White Shark"
# Q4 (Option 1, no pipes)
# Read as : "Calculate the mean of lengths where shark species matches Great White Shark
mean(length_m[shark_species == "Great White Shark"])[1] 5.9
# Q4 (Option 2, with pipe)
length_m[shark_species == "Great White Shark"] |> # Read as: Extract lengths where name matches great white shark AND THEN ...
mean() # Calculate the mean[1] 5.9
Likely pause here
EVR628tools:remotes::install_github("jcvdav/EVR628tools")Start a new script called MPA_analysis, add a comment outline and then load the EVR628tools and tidyverse packages.
Load and inspect the new ?data_MPA
unique() sites are there?unique() years?Create four objects containing:
Hint: Use a combination of subsetting ([ ]), relational (==), and logical operators (&)
# My code will be hereFix the style issues in this code:
mydataframe=data.frame(species=c("Great White","Tiger", "Bull"),
length=c(4.5,3.2, NA))
mean(mydataframe$length,na.rm=TRUE)# My improved code will be hereNote: What’s with than na.rm = TRUE?