INSTRUCTOR NOTE: Code examples should generally build up by modifying the existing code example rather than by retyping the full example.
Conditionals
- Usage:
- Generate
"logical"
:TRUE
if the condition is satisfiedFALSE
if the condition is not satisfied
- Generate
- Operators:
==
,!=
<
,>
<=
,>=
%in%
10 > 5
"aang" == "aang"
3 != 3
"dog" %in% c("cat", "dog", "rabbit")
- Combine:
and
,&
or
,|
5 > 2 & 6 >=10
5 > 2 | 6 >=10
- Vectors of values compared to a single value return one logical per value
c(1, 1, 2, 3, 1) == 1
- Checks each value to see if equal to 1
- This is what subsetting approaches use to subset
- They keep the values where the condition is
TRUE
site = c('a', 'b', 'c', 'd')
state = c('FL', 'FL', 'GA', 'AL')
state == 'FL'
site[state == 'FL']
site[c(TRUE, TRUE, FALSE, FALSE)]
- Used in
dplyr::filter()
and other methods for subsetting data
Do Tasks 1-4 in Choice Operators.
if
statements
- Conditional statements generate logical values to filter inputs.
if
statements use conditional statements to control flow of the program.
if (the conditional statement is TRUE ) {
do something
}
- Example
x = 6
if (x > 5){
x = x^2
}
x
x > 5
isTRUE
, so the code in theif
runsx
is now 6^2 or 36- Change
x
to 4
x = 4
if (x > 5){
x = x^2
}
x
x > 5
isFALSE
, so the code in theif
doesn’t runx
is still 4-
This is not a function, so everything that happens in the if statement influences the global environment
- Different mass calculations for different vegetation types
veg_type <- "tree"
volume <- 16.08
if (veg_type == "tree") {
mass <- 2.65 * volume^0.9
}
mass
Do Task 1 in Basic If Statements.
- Often want to chose one of several options
- Can add more conditions and associated actions with
else if
veg_type <- "grass"
volume <- 16.08
if (veg_type == "tree") {
mass <- 2.65 * volume^0.9
} else if (veg_type == "grass") {
mass <- 0.65 * volume^1.2
}
mass
- Checks the first condition
- If
TRUE
runs that condition’s code and skips the rest -
If not it checks the next one until it runs out of conditions
- Can specify what to do if none of the conditions is
TRUE
usingelse
on its own
veg_type <- "shrub"
volume <- 16.08
if (veg_type == "tree") {
mass <- 2.65 * volume^0.9
} else if (veg_type == "grass") {
mass <- 0.65 * volume^1.2
} else {
mass <- NA
}
mass
Do Tasks 2-3 in Basic If Statements.
Multiple ifs vs else if
- Multiple ifs check each conditional separately
- Executes code of all conditions that are
TRUE
x <- 5
if (x > 2){
x * 2
}
if (x > 4){
x * 4
}
else if
checks each condition sequentially- Executes code for the first condition that is
TRUE
x <- 5
if (x > 2){
x * 2
} else if (x > 4){
x * 4
}
Convert to function
est_mass <- function(volume, veg_type){
if (veg_type == "tree") {
mass <- 2.65 * volume^0.9
} else if (veg_type == "grass") {
mass <- 0.65 * volume^1.2
} else {
print("I don't know how to convert volume to mass for that vegetation type")
mass <- NA
}
return(mass)
}
est_mass(1.6, "tree")
est_mass(1.6, "grass")
est_mass(1.6, "shrub")
Automatically extracting functions
- Can pull code out into functions
- Highlight the code
- Code -> Extract Function
- Provide a name for the function
Nested conditionals
- Sometimes decisions are more complicated
- Can “nest” conditionals inside of one another
est_mass <- function(volume, veg_type, age){
if (veg_type == "tree") {
if (age < 5) {
mass <- 1.6 * volume^0.8
} else {
mass <- 2.65 * volume^0.9
}
} else if (veg_type == "grass" | veg_type == "shrub") {
mass <- 0.65 * volume^1.2
} else {
print("I don't know how to convert volume to mass for that vegetation type")
mass <- NA
}
return(mass)
}
est_mass(1.6, "tree", age = 2)
est_mass(1.6, "shrub", age = 5)
- First checks if the vegetation type is “tree”
- If it is checks to see if it is < 5 years old
- If so does one calculation, if not does another
- But nesting can be difficult to follow so try to minimize it
Assign the rest of the exercises.