if
Statement
- An
if
statement tells R to do a certain task for a certain case. - In English, you would say “If <condition> is true, do <steps>!”
- In R, you would say:
condition
object should be a logical statement/test or an R expression that evaluates to a singleTRUE
orFALSE
.- If
condition
isTRUE
,- R will run all the code that appears between the curly brackets
{}
following theif
statement.
- R will run all the code that appears between the curly brackets
- If
condition
isFALSE
,- R will skip the code between the curly brackets without running it.
Below is an example of an if
statement to make sure a number num
is positive.
- In the case,
num < 0
isTRUE
:
## [1] 2
- In the case,
num < 0
isFALSE
:
## [1] 4
- Quiz 1: What will the following code return?
- Quiz 2: What will the following code return?
- Quiz 3: What will the following code return?
else
Statement
if
statement tells R what to do when the condition isTRUE
.else
statement tells R what to do when the condition isFALSE
.
- Example: Write a function for rounding a number to the nearest whole number.
- Isolate the decimal component with
trunc()
function.
## [1] 0.14
- Use
if else
to round the number (up or down):
## [1] 3
- Write the rounding function:
my_round <- function(a) {
decimal <- a - trunc(a)
if (decimal >= 0.5) {
a <- trunc(a) + 1
} else {
a <- trunc(a)
}
return(a)
}
## [1] 3
## [1] 3
## [1] 5
## [1] 5
- Another example
a <- 1
b <- 1
if (a > b) {
print("A wins!")
} else if (a < b) {
print("B wins!")
} else {
print("Tie.")
}
## [1] "Tie."
Basic Plots
Histogram
- Change the plot title
- Change x-axis label
- Change the y-axis (from frequency) to probability/density
- Change how the histogram is graphed (by changing the column width):
- The argument
breaks
inhist()
can take one of the following:- a vector giving the breakpoints between histogram cells,
- a function to compute the vector of breakpoints,
- a single number giving the number of cells for the histogram,
- a character string naming an algorithm to compute the number of cells (see ‘Details’),
- a function to compute the number of cells.
- The argument
# a vector giving the breakpoints between histogram cells
hist(x = cars$speed, main = "Histogram of Speed", xlab = "Speed (mph)",
probability = TRUE, breaks = c(0, 4, 8, 12, 16, 20, 24, 28, 32))
# a function to compute the vector of breakpoints
hist(x = cars$speed, main = "Histogram of Speed", xlab = "Speed (mph)",
probability = TRUE, breaks = seq(from = 0, to = 30, by = 3))
# a single number giving the number of cells for the histogram
hist(x = cars$speed, main = "Histogram of Speed", xlab = "Speed (mph)",
probability = TRUE, breaks = 10)
# a single number giving the number of cells for the histogram
hist(x = cars$speed, main = "Histogram of Speed", xlab = "Speed (mph)",
probability = TRUE, breaks = "Freedman-Diaconis")
# a function to compute the number of cells
hist(x = cars$speed, main = "Histogram of Speed", xlab = "Speed (mph)",
probability = TRUE, breaks = 5*2)
- Let’s add some color to this histogram!
hist(x = cars$speed, main = "Histogram of Speed", xlab = "Speed (mph)",
probability = TRUE, breaks = seq(from = 0, to = 30, by = 3),
col = "darkorange")
- A bit too much?
hist(x = cars$speed, main = "Histogram of Speed", xlab = "Speed (mph)",
probability = TRUE, breaks = seq(from = 0, to = 30, by = 3),
border = "dodgerblue")
- Make it looks “professional”!
hist(x = cars$speed, main = "Histogram of Speed", xlab = "Speed (mph)",
probability = TRUE, breaks = seq(from = 0, to = 30, by = 3),
col = "lightgrey")
box()
grid()
Boxplot
- Add title and axis label
- Change the orientation of the boxplot
Scatterplot
- Adding plot title, axis labels
plot(x = cars$speed, y = cars$dist, main = "Car Speed vs. Stopping Distance",
xlab = "Speed (mph)", ylab = "Stopping Distance (ft)")
- Add color, change symbol, and other modifications.
- See more details here.
plot(x = cars$speed, y = cars$dist, main = "Car Speed vs. Stopping Distance",
xlab = "Speed (mph)", ylab = "Stopping Distance (ft)",
col = "dodgerblue", pch = 19)
grid()
- Do you notice a difference in the code segment above and below?
plot(dist ~ speed, data = cars,
main = "Car Speed vs. Stopping Distance",
xlab = "Speed (mph)", ylab = "Stopping Distance (ft)",
col = "dodgerblue", pch = 19)
grid()
To-do
- Homework 2 will be published after lecture.
- Read Chapter 11: Loops in Hands-On Programming with R.
References
- Hands-On Programming with R, by Garrett Grolemund.