Working with matrices, lists, and data frames

set.seed(123) # Set seed for reproducibility
# Assign to the variable n_dims a single random integer between 3 and 10.
n_dims <- sample(3:10, 1)
n_dims
## [1] 9
# Create a vector of consecutive integers from 1 to n_dims2.
v <- 1:(n_dims^2) 
v 
##  [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.
v_shuff <- sample(v) # Randomly reshuffle these values
v_shuff
##  [1] 79 51 14 67 42 50 43 81 25 69 57  9 26  7 77 70 36 74 17 75 39 53 12 15 32
## [26] 78 45 68 66 41 10 23 27 54 49 71 38 73 34 29  5  8 59 13 18 33 47 44 21 58
## [51] 60 46 72 28 16 20  6 11 40 22 48 35 63 37 19  2  4 55 52 62  3 31 30 61  1
## [76] 76 65 24 80 56 64
# Create a square matrix with these elements.
mat_shuff <- matrix(v_shuff, nrow=n_dims, ncol=n_dims)

# Print out the matrix
print(mat_shuff)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]   79   69   17   68   38   33   16   37   30
##  [2,]   51   57   75   66   73   47   20   19   61
##  [3,]   14    9   39   41   34   44    6    2    1
##  [4,]   67   26   53   10   29   21   11    4   76
##  [5,]   42    7   12   23    5   58   40   55   65
##  [6,]   50   77   15   27    8   60   22   52   24
##  [7,]   43   70   32   54   59   46   48   62   80
##  [8,]   81   36   78   49   13   72   35    3   56
##  [9,]   25   74   45   71   18   28   63   31   64
# Find a function in r to transpose the matrix.
transposed <- t(mat_shuff)
print(transposed)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]   79   51   14   67   42   50   43   81   25
##  [2,]   69   57    9   26    7   77   70   36   74
##  [3,]   17   75   39   53   12   15   32   78   45
##  [4,]   68   66   41   10   23   27   54   49   71
##  [5,]   38   73   34   29    5    8   59   13   18
##  [6,]   33   47   44   21   58   60   46   72   28
##  [7,]   16   20    6   11   40   22   48   35   63
##  [8,]   37   19    2    4   55   52   62    3   31
##  [9,]   30   61    1   76   65   24   80   56   64
# Calculate the sum and the mean of the elements in the first and last rows
f_row <- sum(mat_shuff[1, ])
mf_row <- mean(mat_shuff[1, ])
l_row <- sum(mat_shuff[n_dims, ])
ml_row <- mean(mat_shuff[n_dims, ])
cat("Sum of first row:", f_row, "\nMean of first row:", mf_row, "\n")
## Sum of first row: 387 
## Mean of first row: 43
cat("Sum of last row:", l_row, "\nMean of last row:", ml_row, "\n")
## Sum of last row: 419 
## Mean of last row: 46.55556
# Read about the eigen() function and use it on your matrix
# Look carefully at the elements of $values and $vectors in the output. What kind of numbers are these?
eigen_result <- eigen(mat_shuff)

# Dig in with the typeof() function to figure out their type.
# 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.
cat("Types of eigen values:", typeof(eigen_result$values), "\n")
## Types of eigen values: complex
cat("Types of eigen vectors:", typeof(eigen_result$vectors), "\n")
## Types of eigen vectors: complex

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. 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. use the typeof() function to confirm the underlying data types of each component in this list 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?

# Create a list with specified elements
set.seed(123) # Ensuring reproducibility
matrix <- matrix(runif(16), 4, 4) # 4x4 matrix with random uniform values
logical <- runif(100) > 0.5 # 100-element logical vector
letters <- sample(letters) # 26 lowercase letters in random order

list <- list(
  matrix_= matrix[2, 2],
  logical_ = logical[2],
  letters_ = letters[2]
)

# Use typeof() to confirm data types
cat("Type of matrix_element:", typeof(list$matrix_element), "\n")
## Type of matrix_element: NULL
cat("Type of logical_element:", typeof(list$logical_element), "\n")
## Type of logical_element: NULL
cat("Type of letters_element:", typeof(list$letters_element), "\n")
## Type of letters_element: NULL
# Combine into a single atomic vector
combined_vector <- c(list$matrix_, list$logical_, list$letters_)
# Check the data type
cat("Data type of combined vector:", typeof(combined_vector), "\n")
## Data type of combined vector: character

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. 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. use the typeof() function to confirm the underlying data types of each component in this list 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?

# Create a data frame as specified
set.seed(123) # Ensuring reproducibility
unis <- runif(26, 0, 10) # 26 random uniform values
letters <- sample(LETTERS) # 26 capital letters in random order
df <- data.frame(unis, letters)

# Replace 4 random rows in my_unis with NA
replace <- sample(1:26, 4)
df$unis[replace] <- NA

# Identify rows with missing values
rows_na <- which(is.na(df$unis))
cat("Rows with NA:", rows_na, "\n")
## Rows with NA: 5 8 12 13
# Re-order data frame
df <- df[order(df$letters), ]

# Calculate column mean, excluding NA
meanunis <- mean(df$unis, na.rm = TRUE)
cat("Mean of unis:", meanunis, "\n")
## Mean of unis: 5.742606