The main purpose of this lab is to practice control structures in R:
if
and else
: testing a condition and
acting on itfor
: execute a loop a fixed number of timeswhile
: execute a loop while a condition is truerepeat
: execute an infinite loop (must break out of it
to stop) • break: break the execution of a loopnext
: skip an iteration of a loopYou will need to modify the code chunks so that the code works within
each of chunk (usually this means modifying anything in ALL CAPS). You
will also need to modify the code outside the code chunk. When you get
the desired result for each step, change Eval=F
to
Eval=T
and knit the document to HTML to make sure it works.
After you complete the lab, you should submit your HTML file of what you
have completed to Canvas before the deadline.
Write code that creates a vector x
that contains
100
random observations from the standard normal
distribution (this is the normal distribution with the mean equal to
0
and the variance equal to 1
). Print out only
the first five random observations in this vector.
#
Write code that replaces the observations in the vector
x
that are greater than or equal to 0
with a
string of characters "non-negative"
and the observations
that are smaller than 0
with a string of characters
"negative"
. Hint: try ifelse()
funtion. Print
out the first five values in this new version of x
.
#
Write for
-Loop to count how many observations in the
vector x
are non-negative and how many observations are
negative. (There are many easier ways to solve this problem. Use
for
-Loop or get 0 points. Use the cat()
function to print out a sentence that states how many non-negative and
negative obervations there are. For example, “The number of non-negative
observations is 32”.
#
Create a \(100000\) by \(10\) matrix A
with the numbers
\(1:1000000\). The first row of this
matrix should be the numbers 1 to 10. The second row of this matrix
should be the numbers 11 to 20. Create a for
-loop that
calculates the sum for each row of the matrix and save the results to a
vector sum_row
and print out the first five values of
sum_row
.
A = matrix(1:1000000, COMPLETE) # DO NOT CHANGE
Verify that your results are consistent with what you obtain with the
built-in rowSums
function.
sum_row_rowSums = as.integer(rowSums(A))
sum_row_rowSums[1:5]
Another common loop structure that is used is the while
loop, which functions much like a for
loop, but will only
run as long as a test condition is TRUE
. Modify your
for
loop from the previous exercise and make it into a
while
loop. Use the identical()
function to
check if the results from the for
loop are the same as the
results from while
loop.
#
Write a for
loop to compute the mean of every column in
mtcars
and save the results to a vector
col_mean
. Ignore missing values when taking the mean.
#
Compute the number of unique values in each column of
iris
and print the results during a loop. Use the
cat()
function to print out the values in a sentence with
the corresponding name of the variable. For example, “The number of
unique values for Sepal.Length is 35”.
names(iris) #DO NOT CHANGE
## [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"