Working with matrices, lists, and data frames

Question #1

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

n_dims <- round(runif(min = 3, max = 10, 1))
print(n_dims)
## [1] 7
  • Create a vector of consecutive integers from 1 to n_dims2.
consecutive_vector <- seq(1,n_dims^2)
consecutive_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.
random <- sample(consecutive_vector)
random
##  [1] 34 19 43 35  3 24 29  9 30 38 40 37 11 12  7 22 23 25 20 14  2 48 49 17 27
## [26] 16 36 10 32 13 28 46 26 18 42 45  5 41  6 39  4  8 15 21 47 44  1 31 33
  • Create a square matrix with these elements.
my_matrix <- matrix(data = random, ncol = 7, nrow=7)
  • print out the matrix.
my_matrix
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]   34    9    7   48   32   45   15
## [2,]   19   30   22   49   13    5   21
## [3,]   43   38   23   17   28   41   47
## [4,]   35   40   25   27   46    6   44
## [5,]    3   37   20   16   26   39    1
## [6,]   24   11   14   36   18    4   31
## [7,]   29   12    2   10   42    8   33
  • find a function in r to transpose the matrix.
my_matrixT <- t(my_matrix)
  • print it out again and note how it has changed.
my_matrixT
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]   34   19   43   35    3   24   29
## [2,]    9   30   38   40   37   11   12
## [3,]    7   22   23   25   20   14    2
## [4,]   48   49   17   27   16   36   10
## [5,]   32   13   28   46   26   18   42
## [6,]   45    5   41    6   39    4    8
## [7,]   15   21   47   44    1   31   33
  • calculate the sum and the mean of the elements in the first row and then the last row.
sum_r1 <- sum(my_matrixT[1,])
sum_r1
## [1] 187
mean_r1 <- mean(my_matrixT[1,])
mean_r1
## [1] 26.71429
sum_r7 <- sum(my_matrixT[7,])
sum_r7
## [1] 192
mean_r7 <- mean(my_matrixT[7,])
mean_r7
## [1] 27.42857
  • read about the eigen() function and use it on your matrix
em <- eigen(my_matrixT)
  • look carefully at the elements of $values and $vectors in the output. What kind of numbers are these?

Imaginary

  • dig in with the typeof() function to figure out their type.
typeof(em)
## [1] "list"
typeof(em$values)
## [1] "complex"
typeof(em$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.

Question #2

Create a list with the following named elements:

  • my_matrix, which is a 4 x 4 matrix filled with random uniform values
my_matrix <- matrix(runif(16), ncol = 4, nrow = 4)
my_matrix
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.7796298 0.1156835 0.7445248 0.06458975
## [2,] 0.6646759 0.6066225 0.1036352 0.28733315
## [3,] 0.6786658 0.3077701 0.4217501 0.99661452
## [4,] 0.2868901 0.1746106 0.7367210 0.24790008
  • 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.
vector <- seq(1:100)
vector
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
##  [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
##  [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
##  [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
##  [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
##  [91]  91  92  93  94  95  96  97  98  99 100
my_logical <- (vector %% 7)>2
my_logical
##   [1] FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE
##  [13]  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE
##  [25]  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
##  [37] FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
##  [49] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE
##  [61]  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE
##  [73]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE
##  [85] FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE
##  [97]  TRUE FALSE FALSE FALSE
  • my_letters, which is a 26-element vector of all the lower-case letters in random order.
my_letters <- sample(letters[1:26])
my_letters
##  [1] "t" "j" "y" "r" "g" "x" "e" "q" "n" "w" "l" "b" "z" "m" "i" "h" "s" "p" "k"
## [20] "o" "v" "f" "u" "a" "c" "d"
my_list <- list(my_matrix, my_logical, my_letters)
my_list
## [[1]]
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.7796298 0.1156835 0.7445248 0.06458975
## [2,] 0.6646759 0.6066225 0.1036352 0.28733315
## [3,] 0.6786658 0.3077701 0.4217501 0.99661452
## [4,] 0.2868901 0.1746106 0.7367210 0.24790008
## 
## [[2]]
##   [1] FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE
##  [13]  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE
##  [25]  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
##  [37] FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
##  [49] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE
##  [61]  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE
##  [73]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE
##  [85] FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE
##  [97]  TRUE FALSE FALSE FALSE
## 
## [[3]]
##  [1] "t" "j" "y" "r" "g" "x" "e" "q" "n" "w" "l" "b" "z" "m" "i" "h" "s" "p" "k"
## [20] "o" "v" "f" "u" "a" "c" "d"

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])
new_list
## [[1]]
## [1] 0.6066225
## 
## [[2]]
## [1] FALSE
## 
## [[3]]
## [1] "j"
  • use the typeof() function to confirm the underlying data types of each component in this list
typeof(new_list[[1]][1]) # double
## [1] "double"
typeof(new_list[[2]][1]) # logical
## [1] "logical"
typeof(new_list[[3]][1]) # character
## [1] "character"
  • combine the underlying elements from the new list into a single atomic vector with the c() function.
single_vec <- c(new_list[[1]], new_list[[2]], new_list[[3]])
single_vec
## [1] "0.606622505700216" "FALSE"             "j"
  • what is the data type of this vector?
typeof(single_vec) # character
## [1] "character"

Question #3

Create a data frame with the two variables (= columns) and 26 cases (= rows) below:

dframe <- data.frame(matrix(ncol = 2, nrow = 26))
dframe
##    X1 X2
## 1  NA NA
## 2  NA NA
## 3  NA NA
## 4  NA NA
## 5  NA NA
## 6  NA NA
## 7  NA NA
## 8  NA NA
## 9  NA NA
## 10 NA NA
## 11 NA NA
## 12 NA NA
## 13 NA NA
## 14 NA NA
## 15 NA NA
## 16 NA NA
## 17 NA NA
## 18 NA NA
## 19 NA NA
## 20 NA NA
## 21 NA NA
## 22 NA NA
## 23 NA NA
## 24 NA NA
## 25 NA NA
## 26 NA NA
  • call the first variable my_unis and fill it with 26 random uniform values from 0 to 10
names(dframe)[1] <- "my_unis"
dframe$my_unis <- runif(26, min=0, max=10)
  • call the second variable my_letters and fill it with 26 capital letters in random order.
names(dframe)[2] <- "my_letters"
dframe[2]
##    my_letters
## 1          NA
## 2          NA
## 3          NA
## 4          NA
## 5          NA
## 6          NA
## 7          NA
## 8          NA
## 9          NA
## 10         NA
## 11         NA
## 12         NA
## 13         NA
## 14         NA
## 15         NA
## 16         NA
## 17         NA
## 18         NA
## 19         NA
## 20         NA
## 21         NA
## 22         NA
## 23         NA
## 24         NA
## 25         NA
## 26         NA
dframe$my_letters <- sample(LETTERS[1:26])
dframe
##      my_unis my_letters
## 1  5.4294918          U
## 2  5.7284447          S
## 3  2.1987910          J
## 4  2.0390946          T
## 5  8.3357139          E
## 6  2.3821388          M
## 7  5.5943047          H
## 8  7.7036383          W
## 9  3.7831975          D
## 10 9.1497352          B
## 11 8.7868074          L
## 12 0.6441515          P
## 13 7.4875529          K
## 14 3.8309958          R
## 15 8.4204234          I
## 16 1.2084581          Y
## 17 1.3238129          V
## 18 8.3567426          O
## 19 3.1347526          Z
## 20 3.8017274          X
## 21 5.0226860          G
## 22 1.5583687          N
## 23 9.7744089          Q
## 24 6.1774606          A
## 25 8.6268151          F
## 26 1.7039598          C
  • 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.
dframe[sample(nrow(dframe), 4), ] <- "NA"
dframe
##              my_unis my_letters
## 1   5.42949182214215          U
## 2   5.72844474110752          S
## 3   2.19879100797698          J
## 4   2.03909457661211          T
## 5   8.33571390248835          E
## 6   2.38213881384581          M
## 7   5.59430471388623          H
## 8   7.70363832591102          W
## 9   3.78319754498079          D
## 10                NA         NA
## 11  8.78680744208395          L
## 12 0.644151524174958          P
## 13  7.48755285283551          K
## 14  3.83099576691166          R
## 15  8.42042337404564          I
## 16                NA         NA
## 17  1.32381290430203          V
## 18  8.35674261907116          O
## 19  3.13475262373686          Z
## 20                NA         NA
## 21                NA         NA
## 22  1.55836873920634          N
## 23  9.77440887363628          Q
## 24  6.17746060946956          A
## 25   8.6268151178956          F
## 26  1.70395977795124          C
  • for the first variable, write a single line of R code to identify which rows have the missing values.
which(dframe$my_unis == 'NA')
## [1] 10 16 20 21
  • re-order the entire data frame to arrange the second variable in alphabetical order
ordered_dframe <- dframe[order(dframe$my_letters), ]
  • calculate the column mean for the first variable.
ordered_dframe$my_unis <- as.numeric(ordered_dframe$my_unis)
## Warning: NAs introduced by coercion
ordered_dframe
##      my_unis my_letters
## 24 6.1774606          A
## 26 1.7039598          C
## 9  3.7831975          D
## 5  8.3357139          E
## 25 8.6268151          F
## 7  5.5943047          H
## 15 8.4204234          I
## 3  2.1987910          J
## 13 7.4875529          K
## 11 8.7868074          L
## 6  2.3821388          M
## 22 1.5583687          N
## 10        NA         NA
## 16        NA         NA
## 20        NA         NA
## 21        NA         NA
## 18 8.3567426          O
## 12 0.6441515          P
## 23 9.7744089          Q
## 14 3.8309958          R
## 2  5.7284447          S
## 4  2.0390946          T
## 1  5.4294918          U
## 17 1.3238129          V
## 8  7.7036383          W
## 19 3.1347526          Z
mean(ordered_dframe$my_unis, na.rm = TRUE)
## [1] 5.137321