Assign to the variable n_dims a single random integer between 3 and 10.
## [1] 8
Create a vector of consecutive integers from 1 to n_dims2
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
## [51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64
Use the sample function to randomly reshuffle these values.
## [1] 44 21 58 54 42 27 37 22 12 10 47 48 31 30 39 49 20 6 26 57 56 1 2 23 33
## [26] 14 59 5 62 8 28 55 63 51 41 29 18 3 7 46 25 38 32 36 13 35 15 9 19 64
## [51] 40 52 4 24 60 43 16 11 50 34 17 53 45 61
create a square matrix with these elements.
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 44 12 20 33 63 25 19 16
## [2,] 21 10 6 14 51 38 64 11
## [3,] 58 47 26 59 41 32 40 50
## [4,] 54 48 57 5 29 36 52 34
## [5,] 42 31 56 62 18 13 4 17
## [6,] 27 30 1 8 3 35 24 53
## [7,] 37 39 2 28 7 15 60 45
## [8,] 22 49 23 55 46 9 43 61
find a function in r to transpose the matrix (+ print it out again and note how it has changed)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 44 21 58 54 42 27 37 22
## [2,] 12 10 47 48 31 30 39 49
## [3,] 20 6 26 57 56 1 2 23
## [4,] 33 14 59 5 62 8 28 55
## [5,] 63 51 41 29 18 3 7 46
## [6,] 25 38 32 36 13 35 15 9
## [7,] 19 64 40 52 4 24 60 43
## [8,] 16 11 50 34 17 53 45 61
calculate the sum and the mean of the elements in the first row and then the last row.
## [1] 305
## [1] 287
## [1] 38.125
## [1] 35.875
read about the eigen() function and use it on your matrix
look carefully at the elements of $values and $vectors in the output. What kind of numbers are these?
Both appear to be a number, either positive or negative plus or minus an imaginary number. However sometimes the imaginary number is 0i
dig in with the typeof() function to figure out their type.
## [1] "complex"
## [1] "complex"
if have set your code up properly, you should be able to re-run it and create a matrix of different size because n_dims will change.
To demonstrate:
## [1] 4
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## [1] 7 8 5 2 10 12 6 11 13 14 9 15 4 3 1 16
## [,1] [,2] [,3] [,4]
## [1,] 7 10 13 4
## [2,] 8 12 14 3
## [3,] 5 6 9 1
## [4,] 2 11 15 16
## [,1] [,2] [,3] [,4]
## [1,] 7 8 5 2
## [2,] 10 12 6 11
## [3,] 13 14 9 15
## [4,] 4 3 1 16
## [1] 22
## [1] 24
Create a list with the following named elements:
my_matrix, which is a 4 x 4 matrix filled with random uniform values my_logical which is a 100-element vector of TRUE or FALSE values. Do this efficiently by setting up a vector of random values and then applying an inequality to it. my_letters, which is a 26-element vector of all the lower-case letters in random order.
my_list <- list(my_matrix <- matrix(runif(16),nrow=4),
my_logical <- runif(100)<0.5,
my_letters <- sample(x=letters, size=26))
Then, complete the following steps:
create a new list, which has the element[2,2] from the matrix, the second element of the logical vector, and the second element of the letters vector.
use the typeof() function to confirm the underlying data types of each component in this list
## [1] "double"
## [1] "logical"
## [1] "character"
combine the underlying elements from the new list into a single atomic vector with the c() function.
## [1] "0.246662406716496" "FALSE" "e"
what is the data type of this vector?
## [1] "character"
Create a data frame with the two variables (= columns) and 26 cases (= rows) below:
call the first variable my_unis and fill it with 26 random uniform values from 0 to 10
call the second variable my_letters and fill it with 26 capital letters in random order.
for the first variable, use a single line of code in R to select 4 random rows and replace the numerical values in those rows with NA.
for the first variable, write a single line of R code to identify which rows have the missing values.
## [1] 7 8 20 24
re-order the entire data frame to arrange the second variable in alphabetical order
calculate the column mean for the first variable.
## [1] 5.264546