$29
You will submit an HTML document created using R Notebook, but you are more than welcome to test your code out in an R script first.
BE SURE TO SAVE YOUR WORK REGULARLY!!!
R as a calculator
Perform the following calculations in R. Be careful about order of operations; you may have to perform some tests to make sure R is behaving the way you expect.
1) e3
2) log(100) ## use base 10
3) ln(4) ## use the natural log
4) 11 - 4/5
5) 510/5
6) 72+5
√
7) 7 ## can you think of two ways to compute this?
Vector Practice
Create vectors with the following specifications:
8) All integers from 1 to 100
9) The odd integers between 1 and 99
10) The numbers 1, 2, 3, 1, 2, 3, 1, 2, 3 etc. [1, 2, 3 repeated 10 times], using the rep() function
11) The numbers 1, 1, 1, 2, 2, 2, 3, 3, 3, . . . , 50, 50, 50, using rep()
12) The numbers 1, 2, 3, . . . , 50, 49, 48, 47, . . . , 1, 0
Without just typing all of the numbers in yourself, create:
13) The fractions 1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10
14) The numbers 1, 8, 27, 64, 125, 216, . . . , 1000
Using the seq() function, create:
15) A sequence of 350 equally spaced numbers from 0 to 0.5
16) A sequence of 100 numbers starting at 1 and spaced by 0.25 Now, create and compute the following:
17) A vector called age with the following ages of people: 17, 19, 16, 34, 65, 72, 47, 52, 10, 12
18) Display the value of the 4th element in the vector (the age of the 4th subject)
1
19) Display the 1st and 10th elements
20) Display all but the 5th element
21) Change the 5th age (65) to 56
22) Create a Boolean (TRUE/FALSE) vector indicating which subjects are over the age of 50. Name this vector “over.50”
23) Create a vector “child” that indicates which subjects are under the age of 18. That is, children should have the value “TRUE” for this vector and adults should have the value “FALSE”
24) Display the ages of the children
25) Display the indices of subjects who are 34 years old
26) Type sum(age > 34). What’s going on here?
27) Count the number of children using the “child” variable
28) Use the length() function to display the age of the last subject
Matrix Practice
29) Create a matrix M as follows M <- matrix(1:16, nrow = 4)
30) Display the element in the 4th row, 3rd column
31) Display the 3rd row of M
32) Display the first, second, and fourth columns of M
33) Use the which() command to display the row and column indices of all elements of M that have the value of 2
34) Create a Boolean matrix, B, indicating which elements of M are greater than 2
35) Write a line of code to count the number of elements in M that are greater than 2
36) Create the following matrix, and name it M2. Display M2.
√
√
√
√
√
1
√
5
√
9
√
13
√2 √6 √10 √14
√
3
√
7
√
11
√
15
4
8
12
16
37) Create the 4 by 4 identity matrix, and name it I. Display I.
1000
0100
0
0
1
0
0
0
0
1
38) Use the which() command to display the row and column indices for the elements of I that are equal to 1
39) Multiple the matrix I times M2 using the matrix multiplication operator, %*%.
40) Now perform the operation I * M2. What happens? This is not standard matrix multiplication, but can be a useful operation to perform.
2
41) Compute M2 + I and M2 - I
42) The R command to compute the transpose of a matrix is t(). Use this command to compute the transpose of M2.
43) Use the diag function to display the diagonal of M2.
44) Use the solve() function to find the inverse of M2. Name the inverse M2.inv. Display M2.inv.
45) Multiply M2 times its inverse (using standard matrix multiplication). Do you get the identity? Note that the inverse is not computed with perfect precision, so the product will not have perfect precision, either. That is, some elements of the product may be slightly different from 0 or slightly different from 1.
46) Round the product that you compute in 45) to 5 decimal places.
3