The goal of this assignment is to continue our exploration of loops, while loops, and if and else statements. Please use the RMD file FIRST_LAST_HW_03.Rmd
to answer your questions. Delete code or answer chunks as necessary. You will submit both the RMD file and the corresponding HTML file from the knitted document.
Note: you may need to search the internet to find answers for some of the questions.
You will be graded as follows:
(20 pts) Have you completed the assignment in its entirety?
(20 pts) Are your responses correct for the subset of randomly graded questions?
(5 pts) CODE DOCUMENTATION. Add comments to nearly every line explaining what the line of code is doing.
(5 pts) Submitted Knitted Document
Is to be completed in Lab 3B. Below is a description of what is necessary from the lab.
Generating a vector filled with Fibonacci Numbers.
Generating a vector filled with Triangular Numbers.
Generating a vector filled with Tetrahedral Numbers.
Create a vector containing only the even numbers from the Fibonacci sequence. Hint: Use loops, if else statements and divide each number by 2 using %%
. Find the sum of all the even Fibonacci numbers?
Create a vector containing only the even numbers from the Triangular sequence. Hint: Use loops, if else statements and divide each number by 2 using %%
. Find the sum of all the even Triangular numbers?
Create a vector containing only the even numbers from the Tetrahedral sequence. Hint: Use loops, if else statements and divide each number by 2 using %%
. Find the sum of all the even Tetrahedral numbers?
Use the following matrix for Part 2 questions:
x <- matrix(1:100, nrow = 10)
x
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 11 21 31 41 51 61 71 81 91
## [2,] 2 12 22 32 42 52 62 72 82 92
## [3,] 3 13 23 33 43 53 63 73 83 93
## [4,] 4 14 24 34 44 54 64 74 84 94
## [5,] 5 15 25 35 45 55 65 75 85 95
## [6,] 6 16 26 36 46 56 66 76 86 96
## [7,] 7 17 27 37 47 57 67 77 87 97
## [8,] 8 18 28 38 48 58 68 78 88 98
## [9,] 9 19 29 39 49 59 69 79 89 99
## [10,] 10 20 30 40 50 60 70 80 90 100
The colSums
function will add all the values in each column of a matrix. Instead of using the colSums
function, write a loop that will produce the same results.
The rowMeans
function will compute the mean for each row in a matrix. Instead of using the rowMeans
function, write a loop that will produce the same results.
Create a logical \(10 \times 10\) matrix indicating whether each element in matrix x
is either even (TRUE) or odd (FALSE).
Write a for loop that iterates over the column names of the mtcars
data set and prints the name with the number of characters in the column name in parentheses.
Example output: "mpg (3)"
.
Use the following functions, and any others you wish: paste
and nchar
functions.
Note: nchar
function counts how many characters are in a string.
Use a while loop to investigate the number of terms required until the product of
1⋅2⋅3⋅4⋅…
reaches above 1 million. How many elements is needed to reach 1 million.
Write a while loop that prints out standard random normal numbers (use rnorm
) but stops (breaks) if you get a number bigger than 1.
Using a while loop, generate a 100 positive values from a standard normal distribution. Your vector should look similar to the vector below.
## [1] 1.011010158 1.537575040 1.341510660 1.064515360 0.066071748 0.029092585
## [7] 1.412751175 0.972670043 0.580242248 0.479153387 0.330257451 0.098798188
## [13] 0.950282616 0.089968514 0.638508945 0.093530123 1.058760927 0.855611994
## [19] 1.222208430 1.415105070 0.365107792 0.271386733 0.365306289 1.671957619
## [25] 1.524889920 2.019418218 0.238135359 0.493558516 1.807449950 1.149605890
## [31] 1.016610155 0.810076008 1.890126258 0.856555795 0.770107022 0.384640207
## [37] 0.174369400 1.031321040 0.571630027 0.971689398 0.869167729 0.097638789
## [43] 0.286020251 1.169119199 0.121613980 0.298412331 0.578623884 0.009499955
## [49] 1.077232718 0.616007377 1.184187335 0.395468968 0.345406276 1.470769854
## [55] 0.976863524 1.327696681 0.008806787 1.355427698 0.972945782 1.490867105
## [61] 1.286643044 0.249618238 1.983017470 0.380277770 0.039665214 1.356740412
## [67] 0.426818729 0.742394426 0.529056350 0.119223489 0.016616357 0.211217746
## [73] 1.855662907 0.562278840 0.648686706 0.655516992 0.351393410 0.091066320
## [79] 0.604324211 0.515949020 2.089040279 0.743307336 1.478835480 1.935790827
## [85] 0.360015804 0.663067820 0.170485200 0.170440051 0.906595671 0.711182024
## [91] 0.345125530 1.273845794 1.115722189 0.531868988 0.904623644 0.351014836
## [97] 0.737518389 0.560867732 1.754492358 0.098945813
Watch the following video https://www.youtube.com/watch?v=HXNhEYqFo0o
What is the difference between loops and recursion?