We can use if
to execute commands only when certain
conditions are met.
if(<value>){
# commands
}
Checks the condition, <value>
and if it is true,
runs the commands. (If <value>
is false, R skips
directly to the end of the curly brackets.)
x <- 1
if(x > 0){
print("positive")
}
## [1] "positive"
Often if
statements are set up like this with some
relational operator as the value
.
Write an if
statement that prints out a short phrase
when x is equal to 1.
The operator else
can be paired with an if
statement. If the if
statement is false, then the
else
code will run. If the if
statement is
true, the if
code will run. (One or the other will run, but
never both.)
x <- -1
if(x > 0){ # check if x > 0
print("x > 0") # runs if x > 0 is true
}else{ # note the placement of the else
print("x <= 0")
}
## [1] "x <= 0"
Copy and paste your code chunk from the last On Your Own prompt. Modify it to print out a different short phrase when x is not equal to 1.
Sometimes we have reason to do a sequence of checks that are all
related, where code will only run if all the previous statements were
false and the current criteria is true. The operator
else if
can be used to implement this.
x = -3
if(x > 0){
type = "positive"
} else if (x < 0) {
type = "negative"
} else if(x == 0){
type = "zero"
} else {
type = "Error"
}
type
## [1] "negative"
What’s the logic here?
Another example with else if
used in a function:
toyfun <- function(X,Y,Do){
if(Do == "Add"){
Z = X+Y
}else if(Do =="Subtract"){
Z = X-Y
}else if(Do =="Multiply"){
Z = X*Y
}else if(Do =="Penguin"){
Z = c("<('' )")
} else{
Z = c(X,Y)
}
return(Z)
}
toyfun(2,4,"Add")
## [1] 6
toyfun(2,4,"Subtract")
## [1] -2
toyfun(2,4,"Penguin")
## [1] "<('' )"
toyfun(2,4,"typo")
## [1] 2 4
Implement a sequence that checks if a logical statement is
TRUE
or FALSE
. Use the final else
statement to allow for the possibility that there is some error and the
input is neither true nor false. If TRUE
, print “yes”; if
FALSE
, print “no”; otherwise print “error”.
Finally, we have a shortcut function that can sometimes help keep our code clean.
ifelse(test, yes, no)
test
is the logical statementyes
is what the code does if test
results
in TRUE
no
is what the code does if test
results
in FALSE
x <- 0
x.info <- ifelse(x < 0, "negative", "nonnegative"); x.info
## [1] "nonnegative"
We can also pass the ifelse
function a vector and it
will check each element individually. For example, in the mtcars data
set, we can find the proportion of cars that have
mpg > 25
and hp > 60
fast_efficient <- ifelse(mtcars$mpg > 25 & mtcars$hp>60, TRUE, FALSE)
sum(fast_efficient)/length(fast_efficient)
## [1] 0.15625
Modify your code from the second On Your Own prompt to use
an ifelse
function.
x <- 105
if(x>0){
if(x>100){
type = "large positive number"
} else {
type = "positive number"
}
} else if(x<0) {
type = "negative number"
} else if(x==0){
type = "zero"
}else {
type = "Error"
}
type
## [1] "large positive number"
We can also nest ifelse
statements by making the
yes
and no
results other ifelse
statements, but this can get tough to follow pretty quickly!
x <- 0
numInf <- ifelse(x > 0, "positive",
ifelse(x < 0, "negative",
ifelse(x == 0, "zero", "error")))
numInf
## [1] "zero"