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

Create a vector of consecutive integers from 1 to n_dims2.

vector_consec <- seq(from=1, to =(n_dims)^2)
print(vector_consec)
##  [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 65 66 67 68 69 70 71 72 73 74 75
## [76] 76 77 78 79 80 81

Use the sample function to randomly reshuffle these values.

sample(x=vector_consec)
##  [1] 57 80 19 51 64 15 12 38 37 79 14  9 66 81 69  8 67 60  3 63 47 78 25 32 52
## [26] 55 76 24 35 49 59  7 28 34 13 50 70 41  1  4 36 56 18 10 42 11 45  6 72 30
## [51] 39 20 68 23 43 53 29  5  2 40 73 48 26 71 27 77 17 75 61 31 21 74 46 54 44
## [76] 65 16 58 22 62 33

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

m <- matrix(data=vector_consec,nrow=n_dims)
print(m)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]    1   10   19   28   37   46   55   64   73
##  [2,]    2   11   20   29   38   47   56   65   74
##  [3,]    3   12   21   30   39   48   57   66   75
##  [4,]    4   13   22   31   40   49   58   67   76
##  [5,]    5   14   23   32   41   50   59   68   77
##  [6,]    6   15   24   33   42   51   60   69   78
##  [7,]    7   16   25   34   43   52   61   70   79
##  [8,]    8   17   26   35   44   53   62   71   80
##  [9,]    9   18   27   36   45   54   63   72   81

find a function in r to transpose the matrix.

tm <- t(m)

print it out again and note how it has changed.

print(tm)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]    1    2    3    4    5    6    7    8    9
##  [2,]   10   11   12   13   14   15   16   17   18
##  [3,]   19   20   21   22   23   24   25   26   27
##  [4,]   28   29   30   31   32   33   34   35   36
##  [5,]   37   38   39   40   41   42   43   44   45
##  [6,]   46   47   48   49   50   51   52   53   54
##  [7,]   55   56   57   58   59   60   61   62   63
##  [8,]   64   65   66   67   68   69   70   71   72
##  [9,]   73   74   75   76   77   78   79   80   81

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

first_row <- tm[1,]
last_row <- tm[n_dims,]

sum(first_row)
## [1] 45
sum(last_row)
## [1] 693
mean(first_row)
## [1] 5
mean(last_row)
## [1] 77

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

eigenm <- eigen(tm, only.values = FALSE, EISPACK = FALSE)
print(eigenm)
## eigen() decomposition
## $values
## [1]  3.817315e+02+0.0000e+00i -1.273146e+01+0.0000e+00i
## [3]  1.425937e-14+0.0000e+00i -5.978131e-15+6.0318e-15i
## [5] -5.978131e-15-6.0318e-15i  7.415305e-15+0.0000e+00i
## [7]  2.129759e-15+0.0000e+00i -1.021053e-15+0.0000e+00i
## [9]  3.741035e-16+0.0000e+00i
## 
## $vectors
##                 [,1]           [,2]           [,3]                    [,4]
##  [1,] -0.04421135+0i  0.53569778+0i  0.22259379+0i  0.08426958+0.04021902i
##  [2,] -0.10624199+0i  0.40683688+0i -0.16126397+0i -0.00086269+0.07863143i
##  [3,] -0.16827263+0i  0.27797597+0i  0.05065003+0i -0.08881057+0.08491135i
##  [4,] -0.23030327+0i  0.14911507+0i  0.18219998+0i -0.13759512-0.04042975i
##  [5,] -0.29233390+0i  0.02025417+0i -0.03771172+0i  0.24623388-0.09199648i
##  [6,] -0.35436454+0i -0.10860673+0i -0.85708057+0i  0.29448602-0.39485081i
##  [7,] -0.41639518+0i -0.23746763+0i  0.32750401+0i -0.53093082+0.00000000i
##  [8,] -0.47842582+0i -0.36632853+0i  0.20027789+0i -0.25381064+0.37304691i
##  [9,] -0.54045645+0i -0.49518944+0i  0.07283055+0i  0.38702037-0.04953166i
##                          [,5]            [,6]           [,7]           [,8]
##  [1,]  0.08426958-0.04021902i -0.320097377+0i -0.18327780+0i -0.02225211+0i
##  [2,] -0.00086269-0.07863143i  0.008426601+0i -0.03945545+0i  0.03803621+0i
##  [3,] -0.08881057-0.08491135i  0.231650006+0i  0.16179535+0i  0.32957228+0i
##  [4,] -0.13759512+0.04042975i -0.053176583+0i  0.06124515+0i -0.05334482+0i
##  [5,]  0.24623388+0.09199648i  0.307289077+0i  0.62885362+0i -0.77034254+0i
##  [6,]  0.29448602+0.39485081i  0.337197061+0i -0.44170913+0i  0.07732643+0i
##  [7,] -0.53093082+0.00000000i -0.630251601+0i -0.50831841+0i  0.51989670+0i
##  [8,] -0.25381064-0.37304691i  0.397531401+0i  0.29176242+0i  0.01065132+0i
##  [9,]  0.38702037+0.04953166i -0.278568586+0i  0.02910425+0i -0.12954347+0i
##                 [,9]
##  [1,] -0.04079522+0i
##  [2,] -0.03295809+0i
##  [3,] -0.05151749+0i
##  [4,] -0.04138519+0i
##  [5,]  0.75624159+0i
##  [6,] -0.36815250+0i
##  [7,] -0.49723601+0i
##  [8,]  0.14706246+0i
##  [9,]  0.12874044+0i

look carefully at the elements of $values and $vectors. What kind of numbers are these? Dig in with the typeof() function to figure out their type.

typeof(eigenm$values)
## [1] "complex"
typeof(eigenm$vectors)
## [1] "complex"

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_data <- runif(16)
my_matrix <- matrix(data=my_data,nrow=4)
print(my_matrix)
##            [,1]       [,2]       [,3]      [,4]
## [1,] 0.64493485 0.32470619 0.70563974 0.9774175
## [2,] 0.87039043 0.35358762 0.59947258 0.1142316
## [3,] 0.06350321 0.27122261 0.76214068 0.6031014
## [4,] 0.24763675 0.07087446 0.08179467 0.5115237

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.

logical <- sample(0:100, 100, replace=TRUE)
print(logical)
##   [1] 26 44  5 79  7 79 45 43 42 50 33 63 80 38 69 87 20 63 49 87 13 11 91 75 48
##  [26] 80 32 58 90 88 81 35 13 21 18  9 97 44 33 39 86 55 37 95  9 66 87 60 63 18
##  [51] 21 52  4 86 40 23 98  0 71 59 43  6 83 58 25 29 38  0 85 88 59 22 77 99 86
##  [76] 97 46 23 75 38 45  0 35 21  0 87 13 38 57 92 68 98 26 22 70 75 80 20 37 74
my_logical <- logical<50
print(my_logical)
##   [1]  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE
##  [13] FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE
##  [25]  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [37] FALSE  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE
##  [49] FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE
##  [61]  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE
##  [73] FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [85]  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE
##  [97] FALSE  TRUE  TRUE FALSE

my_letters, which is a 26-element vector of all the lower-case letters in random order.

my_letters <- sample(letters, size=26)
print(my_letters)
##  [1] "h" "t" "m" "v" "o" "w" "j" "a" "z" "u" "i" "e" "p" "d" "f" "r" "g" "n" "s"
## [20] "l" "k" "y" "x" "c" "q" "b"

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.

my_list <- list(my_matrix[2,2],my_logical[2],my_letters[2])
print(my_list)
## [[1]]
## [1] 0.3535876
## 
## [[2]]
## [1] TRUE
## 
## [[3]]
## [1] "t"

use the typeof() function to confirm the underlying data types of each component in this list

typeof(my_list[[1]])
## [1] "double"
typeof(my_list[[2]])
## [1] "logical"
typeof(my_list[[3]])
## [1] "character"

combine the underlying elements from the new list into a single atomic vector with the c() function. What is the data type of this vector?

condensed_list <- c(my_list[[1]],my_list[[2]],my_list[[3]])
print(condensed_list)
## [1] "0.353587621590123" "TRUE"              "t"
typeof(condensed_list)
## [1] "character"

Problem 3

Create a data frame with two variables (= columns) and 26 cases (= rows).

call the first variable my_unis and fill it with 26 random uniform values from 0 to 10

my_unis <- runif(n=26,min=1,max=10)
print(my_unis)
##  [1] 2.768700 7.349744 5.582243 9.708352 2.679995 2.693027 3.511938 7.886325
##  [9] 3.159215 5.533404 2.887600 4.349876 8.326151 9.261414 4.742808 6.856755
## [17] 8.109409 9.394490 5.300257 2.832395 5.064894 6.334344 8.249642 5.624872
## [25] 6.285125 6.380251

call the second variable my_letters and fill it with 26 capital letters in random order.

my_letters <- sample(LETTERS, size=26)
print(my_letters)
##  [1] "Q" "J" "F" "W" "Z" "X" "V" "L" "K" "N" "U" "I" "B" "P" "H" "M" "C" "G" "D"
## [20] "Y" "O" "S" "R" "T" "E" "A"
dataFrame <- data.frame(my_unis,my_letters)
print(dataFrame)
##     my_unis my_letters
## 1  2.768700          Q
## 2  7.349744          J
## 3  5.582243          F
## 4  9.708352          W
## 5  2.679995          Z
## 6  2.693027          X
## 7  3.511938          V
## 8  7.886325          L
## 9  3.159215          K
## 10 5.533404          N
## 11 2.887600          U
## 12 4.349876          I
## 13 8.326151          B
## 14 9.261414          P
## 15 4.742808          H
## 16 6.856755          M
## 17 8.109409          C
## 18 9.394490          G
## 19 5.300257          D
## 20 2.832395          Y
## 21 5.064894          O
## 22 6.334344          S
## 23 8.249642          R
## 24 5.624872          T
## 25 6.285125          E
## 26 6.380251          A

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.

dataFrame[sample(26, size = 4),1] <- NA
print(dataFrame)
##     my_unis my_letters
## 1  2.768700          Q
## 2  7.349744          J
## 3  5.582243          F
## 4  9.708352          W
## 5  2.679995          Z
## 6  2.693027          X
## 7  3.511938          V
## 8        NA          L
## 9  3.159215          K
## 10 5.533404          N
## 11       NA          U
## 12 4.349876          I
## 13 8.326151          B
## 14       NA          P
## 15 4.742808          H
## 16       NA          M
## 17 8.109409          C
## 18 9.394490          G
## 19 5.300257          D
## 20 2.832395          Y
## 21 5.064894          O
## 22 6.334344          S
## 23 8.249642          R
## 24 5.624872          T
## 25 6.285125          E
## 26 6.380251          A

for the first variable, write a single line of R code to identify which rows have the missing values.

dataFrame[!complete.cases(dataFrame),]
##    my_unis my_letters
## 8       NA          L
## 11      NA          U
## 14      NA          P
## 16      NA          M

for the second variable, sort it in alphabetical order

dataFrame <- dataFrame[order(my_letters),]
print(dataFrame)
##     my_unis my_letters
## 26 6.380251          A
## 13 8.326151          B
## 17 8.109409          C
## 19 5.300257          D
## 25 6.285125          E
## 3  5.582243          F
## 18 9.394490          G
## 15 4.742808          H
## 12 4.349876          I
## 2  7.349744          J
## 9  3.159215          K
## 8        NA          L
## 16       NA          M
## 10 5.533404          N
## 21 5.064894          O
## 14       NA          P
## 1  2.768700          Q
## 23 8.249642          R
## 22 6.334344          S
## 24 5.624872          T
## 11       NA          U
## 7  3.511938          V
## 4  9.708352          W
## 6  2.693027          X
## 20 2.832395          Y
## 5  2.679995          Z

calculate the column mean for the first variable.

mean(dataFrame$my_unis[!is.na(dataFrame$my_unis)])
## [1] 5.635506