Homework 5

Problem 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] 8

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 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.

vec_a2 <- sample(x=vec_a)
print(vec_a2)
##  [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.

matrix_a <- matrix(data=vec_a2,nrow=n_dims)
print(matrix_a)
##      [,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)

transposed_matrix_a <- t(matrix_a)
print(transposed_matrix_a)
##      [,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.

sum(transposed_matrix_a[1, ])
## [1] 305
sum(transposed_matrix_a[n_dims, ])
## [1] 287
mean(transposed_matrix_a[1, ])
## [1] 38.125
mean(transposed_matrix_a[n_dims, ])
## [1] 35.875

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] 4
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
vec_a2 <- sample(x=vec_a)
print(vec_a2)
##  [1]  7  8  5  2 10 12  6 11 13 14  9 15  4  3  1 16
matrix_a <- matrix(data=vec_a2,nrow=n_dims)
print(matrix_a)
##      [,1] [,2] [,3] [,4]
## [1,]    7   10   13    4
## [2,]    8   12   14    3
## [3,]    5    6    9    1
## [4,]    2   11   15   16
transposed_matrix_a <- t(matrix_a)
print(transposed_matrix_a)
##      [,1] [,2] [,3] [,4]
## [1,]    7    8    5    2
## [2,]   10   12    6   11
## [3,]   13   14    9   15
## [4,]    4    3    1   16
sum(transposed_matrix_a[1, ])
## [1] 22
sum(transposed_matrix_a[n_dims, ])
## [1] 24

Problem 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.246662406716496" "FALSE"             "e"

what is the data type of this vector?

typeof(new_vec)
## [1] "character"

Problem 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]  7  8 20 24

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] 5.264546