Problem 1

Assign to the variable n_dims a single random integer between 3 and 10.

sequence <- 3:10
#print(sequence)
n_dims <- sample(sequence, 1)
print(n_dims)
## [1] 7

Create a vector of consecutive integers from 1 to n_dims^2

consec_vector <- 1:n_dims^2
print(consec_vector)
##  [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

Use the sample function to randomly reshuffle these values.

consec_vector <- sample(consec_vector,length(consec_vector))
print(consec_vector)
##  [1]  9 16  7 35 42 27 33 22  3 46 47 39 10 48 41 29 38 32 26  2 30 28 37 40 11
## [26] 19 15 43 45 34 49 21 13  5 18  1 24  6 12 31 36 23  8  4 20 44 17 25 14

create a square matrix with these elements and print out the matrix.

sqr_matrix <- matrix(data=consec_vector,nrow=sqrt(length(consec_vector)))
print(sqr_matrix)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]    9   22   41   28   45    1    8
## [2,]   16    3   29   37   34   24    4
## [3,]    7   46   38   40   49    6   20
## [4,]   35   47   32   11   21   12   44
## [5,]   42   39   26   19   13   31   17
## [6,]   27   10    2   15    5   36   25
## [7,]   33   48   30   43   18   23   14

find a function in r to transpose the matrix and print again showing change

sqr_matrix <- t(sqr_matrix)
print(sqr_matrix)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]    9   16    7   35   42   27   33
## [2,]   22    3   46   47   39   10   48
## [3,]   41   29   38   32   26    2   30
## [4,]   28   37   40   11   19   15   43
## [5,]   45   34   49   21   13    5   18
## [6,]    1   24    6   12   31   36   23
## [7,]    8    4   20   44   17   25   14

calculate the sum and the mean of the elements in the first row and then the last row.

print(sqr_matrix[1,])
## [1]  9 16  7 35 42 27 33
#sum of row 1
sum(sqr_matrix[1,])
## [1] 169
#mean of row 1
mean(sqr_matrix[1,])
## [1] 24.14286

read about the eigen() function and use it on your matrix

print(eigen(sqr_matrix))
## eigen() decomposition
## $values
## [1] 176.526109+0.000000i -34.366899+2.639243i -34.366899-2.639243i
## [4]  13.257977+7.401376i  13.257977-7.401376i -12.873566+0.000000i
## [7]   2.565301+0.000000i
## 
## $vectors
##               [,1]                     [,2]                     [,3]
## [1,] -0.3478169+0i -0.13562154+0.042421187i -0.13562154-0.042421187i
## [2,] -0.4540813+0i -0.07032176+0.108644899i -0.07032176-0.108644899i
## [3,] -0.4333481+0i  0.06631031-0.029350135i  0.06631031+0.029350135i
## [4,] -0.4085323+0i  0.67758459-0.019404958i  0.67758459+0.019404958i
## [5,] -0.4111312+0i  0.06657153-0.076194262i  0.06657153+0.076194262i
## [6,] -0.2693920+0i  0.10110867+0.005513019i  0.10110867-0.005513019i
## [7,] -0.2766649+0i -0.69123930+0.000000000i -0.69123930+0.000000000i
##                         [,4]                    [,5]          [,6]
## [1,]  0.36152763-0.11545008i  0.36152763+0.11545008i  0.4392013+0i
## [2,] -0.25777618+0.05891291i -0.25777618-0.05891291i -0.6430948+0i
## [3,] -0.35481405+0.13883977i -0.35481405-0.13883977i  0.1312468+0i
## [4,] -0.01505745-0.09629637i -0.01505745+0.09629637i -0.4069492+0i
## [5,] -0.35577088+0.26794139i -0.35577088-0.26794139i -0.1149803+0i
## [6,]  0.53716951+0.00000000i  0.53716951+0.00000000i  0.3169832+0i
## [7,]  0.30205435-0.23071008i  0.30205435+0.23071008i  0.3114467+0i
##               [,7]
## [1,]  0.6545305+0i
## [2,] -0.4668587+0i
## [3,] -0.2742568+0i
## [4,] -0.1947045+0i
## [5,] -0.1336802+0i
## [6,]  0.4381738+0i
## [7,]  0.1750414+0i
eigen_result <- eigen(sqr_matrix)

look carefully at the elements of $values and $vectors in the output. What kind of numbers are these?

These numbers are complex: number + imaginary number ### dig in with the typeof() function to figure out their type.

print(typeof(eigen_result[1][1])) #function returns a list of 2 items
## [1] "list"
print(eigen_result[1]) #print the first list item
## $values
## [1] 176.526109+0.000000i -34.366899+2.639243i -34.366899-2.639243i
## [4]  13.257977+7.401376i  13.257977-7.401376i -12.873566+0.000000i
## [7]   2.565301+0.000000i
print(eigen_result[[1]]) #print the value of the first list element
## [1] 176.526109+0.000000i -34.366899+2.639243i -34.366899-2.639243i
## [4]  13.257977+7.401376i  13.257977-7.401376i -12.873566+0.000000i
## [7]   2.565301+0.000000i
print(eigen_result[[1]][1]) #print the first item in that structure
## [1] 176.5261+0i
print(typeof(eigen_result[[1]][1]))
## [1] "complex"
print(eigen_result[[1]][[1]])
## [1] 176.5261+0i
print(typeof(eigen_result[[1]][[1]]))
## [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.

True

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_matrix <- matrix(runif(16),4)
#print(my_matrix)

rand_vals <- runif(100)
my_logical <- rand_vals > runif(1)
#print(my_logical)

my_letters <- sample(letters,26)
#print(my_letters)

my_list <- list(my_matrix,my_logical,my_letters)
print(my_list)
## [[1]]
##           [,1]        [,2]      [,3]       [,4]
## [1,] 0.1081851 0.798936629 0.8434662 0.08387665
## [2,] 0.1030491 0.508851316 0.7393030 0.71900988
## [3,] 0.7191971 0.841775447 0.1869178 0.52471538
## [4,] 0.4771738 0.002234618 0.4877817 0.18911036
## 
## [[2]]
##   [1]  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE
##  [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [25]  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE
##  [37]  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
##  [49] FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE
##  [61] FALSE  TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE
##  [73]  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE
##  [85]  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
##  [97] FALSE FALSE  TRUE  TRUE
## 
## [[3]]
##  [1] "o" "a" "w" "l" "z" "j" "k" "v" "c" "u" "b" "i" "q" "x" "r" "d" "h" "n" "s"
## [20] "t" "f" "m" "g" "e" "p" "y"

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_list[[1]][2,2],my_list[[2]][2],my_list[[3]][2])
print(new_list)
## [[1]]
## [1] 0.5088513
## 
## [[2]]
## [1] FALSE
## 
## [[3]]
## [1] "a"

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.

atom_vector <- c(new_list[[1]],new_list[[2]],new_list[[3]])
print(atom_vector)
## [1] "0.508851316291839" "FALSE"             "a"

what is the data type of this vector?

typeof(atom_vector)
## [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.
my_unis <- runif(26,0,10)
print(my_unis)
##  [1] 2.0194908 5.0042691 7.2977545 0.4172426 7.4244742 2.3466924 7.2799519
##  [8] 3.2718691 0.9689014 6.6524540 3.9835167 1.2149711 0.7751011 1.9941195
## [15] 7.8664254 9.9671267 5.4081147 9.2207439 0.9564827 8.1671094 8.0721384
## [22] 4.8310031 7.4044498 1.7230947 1.3875290 1.6308949
my_letters <- sample(letters,26)
d_frame <- data.frame(my_unis,my_letters)
print(d_frame)
##      my_unis my_letters
## 1  2.0194908          d
## 2  5.0042691          e
## 3  7.2977545          a
## 4  0.4172426          v
## 5  7.4244742          w
## 6  2.3466924          m
## 7  7.2799519          o
## 8  3.2718691          j
## 9  0.9689014          q
## 10 6.6524540          b
## 11 3.9835167          p
## 12 1.2149711          c
## 13 0.7751011          h
## 14 1.9941195          r
## 15 7.8664254          x
## 16 9.9671267          s
## 17 5.4081147          g
## 18 9.2207439          u
## 19 0.9564827          l
## 20 8.1671094          t
## 21 8.0721384          f
## 22 4.8310031          n
## 23 7.4044498          k
## 24 1.7230947          i
## 25 1.3875290          z
## 26 1.6308949          y

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(1:26,4),1] <- NA
print(d_frame)
##      my_unis my_letters
## 1  2.0194908          d
## 2         NA          e
## 3  7.2977545          a
## 4  0.4172426          v
## 5  7.4244742          w
## 6  2.3466924          m
## 7  7.2799519          o
## 8  3.2718691          j
## 9  0.9689014          q
## 10 6.6524540          b
## 11 3.9835167          p
## 12 1.2149711          c
## 13 0.7751011          h
## 14 1.9941195          r
## 15        NA          x
## 16 9.9671267          s
## 17 5.4081147          g
## 18 9.2207439          u
## 19 0.9564827          l
## 20 8.1671094          t
## 21 8.0721384          f
## 22        NA          n
## 23 7.4044498          k
## 24        NA          i
## 25 1.3875290          z
## 26 1.6308949          y

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]))
## [1]  2 15 22 24

re-order the entire data frame to arrange the second variable in alphabetical order

calculate the column mean for the first variable.

print(d_frame[complete.cases(d_frame[[1]]),])
##      my_unis my_letters
## 1  2.0194908          d
## 3  7.2977545          a
## 4  0.4172426          v
## 5  7.4244742          w
## 6  2.3466924          m
## 7  7.2799519          o
## 8  3.2718691          j
## 9  0.9689014          q
## 10 6.6524540          b
## 11 3.9835167          p
## 12 1.2149711          c
## 13 0.7751011          h
## 14 1.9941195          r
## 16 9.9671267          s
## 17 5.4081147          g
## 18 9.2207439          u
## 19 0.9564827          l
## 20 8.1671094          t
## 21 8.0721384          f
## 23 7.4044498          k
## 25 1.3875290          z
## 26 1.6308949          y
clean <- d_frame[complete.cases(d_frame[[1]]),]
print(clean)
##      my_unis my_letters
## 1  2.0194908          d
## 3  7.2977545          a
## 4  0.4172426          v
## 5  7.4244742          w
## 6  2.3466924          m
## 7  7.2799519          o
## 8  3.2718691          j
## 9  0.9689014          q
## 10 6.6524540          b
## 11 3.9835167          p
## 12 1.2149711          c
## 13 0.7751011          h
## 14 1.9941195          r
## 16 9.9671267          s
## 17 5.4081147          g
## 18 9.2207439          u
## 19 0.9564827          l
## 20 8.1671094          t
## 21 8.0721384          f
## 23 7.4044498          k
## 25 1.3875290          z
## 26 1.6308949          y
clean[[1]]
##  [1] 2.0194908 7.2977545 0.4172426 7.4244742 2.3466924 7.2799519 3.2718691
##  [8] 0.9689014 6.6524540 3.9835167 1.2149711 0.7751011 1.9941195 9.9671267
## [15] 5.4081147 9.2207439 0.9564827 8.1671094 8.0721384 7.4044498 1.3875290
## [22] 1.6308949
mean(clean[[1]])
## [1] 4.448233