#1.
Assign to the variable n_dims a single random integer between 3 and 10.
n_dims <- sample(3:10,1)
print(n_dims)
## [1] 6
Create a vector of consecutive integers from 1 to n_dims2
vec_a <- seq(from=1, to=(n_dims^2))
print(vec_a)
## [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
Use the sample function to randomly reshuffle these values.
vec_a2 <- sample(x=vec_a)
print(vec_a2)
## [1] 11 35 15 36 16 6 12 18 21 3 2 29 1 24 17 26 32 9 31 34 10 25 5 30 28
## [26] 27 22 19 7 8 33 14 4 20 23 13
create a square matrix with these elements.
matrix_a <- matrix(data=vec_a2,nrow=n_dims)
print(matrix_a)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 11 12 1 31 28 33
## [2,] 35 18 24 34 27 14
## [3,] 15 21 17 10 22 4
## [4,] 36 3 26 25 19 20
## [5,] 16 2 32 5 7 23
## [6,] 6 29 9 30 8 13
find a function in r to transpose the matrix (+ print it out again and note how it has changed)
transposed_matrix_a <- t(matrix_a)
print(transposed_matrix_a)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 11 35 15 36 16 6
## [2,] 12 18 21 3 2 29
## [3,] 1 24 17 26 32 9
## [4,] 31 34 10 25 5 30
## [5,] 28 27 22 19 7 8
## [6,] 33 14 4 20 23 13
calculate the sum and the mean of the elements in the first row and then the last row.
sum(transposed_matrix_a[1, ])
## [1] 119
sum(transposed_matrix_a[n_dims, ])
## [1] 107
mean(transposed_matrix_a[1, ])
## [1] 19.83333
mean(transposed_matrix_a[n_dims, ])
## [1] 17.83333
read about the eigen() function and use it on your matrix
decomp <- eigen(transposed_matrix_a)
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.
typeof(decomp$values)
## [1] "complex"
typeof(decomp$vectors)
## [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:
n_dims <- sample(3:10,1)
print(n_dims)
## [1] 8
vec_a <- seq(from=1, to=(n_dims^2))
print(vec_a)
## [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
vec_a2 <- sample(x=vec_a)
print(vec_a2)
## [1] 8 17 61 32 52 36 50 14 34 59 2 47 46 19 25 11 57 63 29 60 10 20 40 43 5
## [26] 33 24 64 44 48 37 26 4 16 38 54 18 22 7 51 23 42 45 15 9 21 12 55 62 58
## [51] 53 30 49 6 31 3 39 27 13 28 35 41 1 56
matrix_a <- matrix(data=vec_a2,nrow=n_dims)
print(matrix_a)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 8 34 57 5 4 23 62 39
## [2,] 17 59 63 33 16 42 58 27
## [3,] 61 2 29 24 38 45 53 13
## [4,] 32 47 60 64 54 15 30 28
## [5,] 52 46 10 44 18 9 49 35
## [6,] 36 19 20 48 22 21 6 41
## [7,] 50 25 40 37 7 12 31 1
## [8,] 14 11 43 26 51 55 3 56
transposed_matrix_a <- t(matrix_a)
print(transposed_matrix_a)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 8 17 61 32 52 36 50 14
## [2,] 34 59 2 47 46 19 25 11
## [3,] 57 63 29 60 10 20 40 43
## [4,] 5 33 24 64 44 48 37 26
## [5,] 4 16 38 54 18 22 7 51
## [6,] 23 42 45 15 9 21 12 55
## [7,] 62 58 53 30 49 6 31 3
## [8,] 39 27 13 28 35 41 1 56
sum(transposed_matrix_a[1, ])
## [1] 270
sum(transposed_matrix_a[n_dims, ])
## [1] 240
#2.
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.
new_list <- list(my_matrix[[2,2]], my_logical[2], my_letters[2])
use the typeof() function to confirm the underlying data types of each component in this list
typeof(new_list[[1]])
## [1] "double"
typeof(new_list[[2]])
## [1] "logical"
typeof(new_list[[3]])
## [1] "character"
combine the underlying elements from the new list into a single atomic vector with the c() function.
new_vec <- c(new_list[[1]],new_list[[2]],new_list[[3]])
print(new_vec)
## [1] "0.756987712578848" "FALSE" "q"
what is the data type of this vector?
typeof(new_vec)
## [1] "character"
#3. 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.
d_frame <- data.frame(my_unis=runif(n=26,min=0, max=10),
my_letters=sample(x=LETTERS, size=26))
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.
d_frame[sample(x=1:26,4),1] <- NA
for the first variable, write a single line of R code to identify which rows have the missing values.
which(!complete.cases(d_frame))
## [1] 12 18 22 26
re-order the entire data frame to arrange the second variable in alphabetical order
d_frame <- d_frame[order(d_frame$my_letters), ]
calculate the column mean for the first variable.
mean(d_frame$my_unis, na.rm = TRUE)
## [1] 4.86653