Task 1: Matrix Operations

Assign a Random Integer for the Matrix

n_dims <- sample(3:10, size=1)
n_dims
## [1] 6

Generate a Randomized Matrix

# Create a sequence from 1 to n_dims^2
vector <- seq(1, n_dims^2)
# Shuffle the sequence to randomize the values
vector <- sample(vector)
# Convert the vector into a square matrix
matrix1 <- matrix(vector, nrow=n_dims)
print(matrix1)
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]   16    8   31   21   19   13
## [2,]   35    5   26   18   14   28
## [3,]   20   12   29   11   22   33
## [4,]   17   27    9   10    3    4
## [5,]   36   15   25    1   24   30
## [6,]    2   23    7   34    6   32

Apply a Matrix Transposition

# Transpose the matrix so that rows become columns and vice versa
transposed_matrix <- t(matrix1)
print(transposed_matrix)
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]   16   35   20   17   36    2
## [2,]    8    5   12   27   15   23
## [3,]   31   26   29    9   25    7
## [4,]   21   18   11   10    1   34
## [5,]   19   14   22    3   24    6
## [6,]   13   28   33    4   30   32

Compute Basic Statistics on Specific Rows

# Find the mean and sum for the first row
first_row_mean <- mean(matrix1[1,])
first_row_sum <- sum(matrix1[1,])
# Find the mean and sum for the last row
last_row_mean <- mean(matrix1[n_dims,])
last_row_sum <- sum(matrix1[n_dims,])

list(first_row_mean, first_row_sum, last_row_mean, last_row_sum)
## [[1]]
## [1] 18
## 
## [[2]]
## [1] 108
## 
## [[3]]
## [1] 17.33333
## 
## [[4]]
## [1] 104

Perform Eigenvalue Analysis

# Compute the eigenvalues and eigenvectors of the matrix
matrix_eigen <- eigen(matrix1)
print(matrix_eigen)
## eigen() decomposition
## $values
## [1] 110.232633+ 0.00000i  16.847101+15.84496i  16.847101-15.84496i
## [4] -16.203654+12.32779i -16.203654-12.32779i   4.480475+ 0.00000i
## 
## $vectors
##               [,1]                    [,2]                    [,3]
## [1,] -0.3956568+0i -0.36224899+0.11537060i -0.36224899-0.11537060i
## [2,] -0.4462432+0i -0.08278642+0.05807819i -0.08278642-0.05807819i
## [3,] -0.4683580+0i -0.04773189-0.34613116i -0.04773189+0.34613116i
## [4,] -0.2575585+0i -0.16417121+0.36854866i -0.16417121-0.36854866i
## [5,] -0.4975268+0i -0.27979599-0.16400901i -0.27979599+0.16400901i
## [6,] -0.3333080+0i  0.67467656+0.00000000i  0.67467656+0.00000000i
##                         [,4]                    [,5]           [,6]
## [1,]  0.15448095-0.37837229i  0.15448095+0.37837229i  0.22156980+0i
## [2,] -0.54345509+0.00000000i -0.54345509+0.00000000i -0.04104432+0i
## [3,] -0.02881074+0.04804892i -0.02881074-0.04804892i -0.55145302+0i
## [4,]  0.29894305+0.35885295i  0.29894305-0.35885295i  0.21519005+0i
## [5,] -0.16292965+0.44839485i -0.16292965-0.44839485i  0.72673484+0i
## [6,]  0.13448285-0.26581160i  0.13448285+0.26581160i -0.26584106+0i

Verify Data Types of Eigenvalues and Eigenvectors

# Check the data types of eigenvalues and eigenvectors
typeof(matrix_eigen$values)
## [1] "complex"
typeof(matrix_eigen$vectors)
## [1] "complex"

Task 2: Creating and Manipulating Lists

Build a List Containing Different Data Types

# Create a 4x4 matrix with random uniform values
my_matrix <- matrix(runif(16), nrow=4)
# Generate a logical vector of TRUE/FALSE values
my_logical <- (runif(100) > .8)
# Generate a randomized sequence of lowercase letters
my_letters <- sample(letters)

Extract and Store Specific Elements in a New List

# Create a new list extracting specific values from each component
list1 <- list(my_matrix, my_logical, my_letters)
new_list <- list(my_matrix[2,2], my_logical[2], my_letters[2])

Check Data Types of Extracted Elements

# Verify the type of each extracted element
typeof(new_list[[1]])
## [1] "double"
typeof(new_list[[2]])
## [1] "logical"
typeof(new_list[[3]])
## [1] "character"

Merge Elements into a Single Vector

# Combine extracted elements into a single atomic vector
combined_elements_vector <- c(new_list[[1]], new_list[[2]], new_list[[3]])
print(combined_elements_vector)
## [1] "0.37487425445579" "FALSE"            "l"
# Determine the data type of the combined vector
typeof(combined_elements_vector)
## [1] "character"

Task 3: Data Frames and Handling Missing Data

Construct a Data Frame

# Create a data frame with two columns
# First column: Random uniform values between 0 and 10
# Second column: Randomly ordered capital letters
dataframe <- data.frame(
  my_unis = runif(26, min=0, max=10), 
  my_letters = sample(LETTERS)
)
print(dataframe)
##      my_unis my_letters
## 1  6.2961703          T
## 2  7.3600030          Q
## 3  3.7422295          G
## 4  7.0522310          B
## 5  0.5344522          V
## 6  5.8375806          M
## 7  2.6416661          X
## 8  1.8002406          D
## 9  2.8757399          C
## 10 6.0383798          Y
## 11 4.1973116          U
## 12 6.3608842          H
## 13 9.4769303          S
## 14 5.2583477          K
## 15 2.4927578          O
## 16 7.4304421          R
## 17 5.3039563          A
## 18 1.3633687          E
## 19 2.4028119          Z
## 20 1.1826371          F
## 21 1.0744556          I
## 22 6.5077801          W
## 23 7.3759481          J
## 24 4.4720011          P
## 25 1.4671458          L
## 26 8.4052260          N

Introduce Missing Values

# Replace values in four randomly chosen rows with NA
dataframe[sample(1:26, size=4), 1] <- NA

Identify Positions of Missing Data

# Locate the rows where NA values appear
which(is.na(dataframe$my_unis))
## [1]  4 11 20 24

Reorder Data by Alphabetical Order

# Arrange data based on the alphabetical order of the letter column
dataframe <- dataframe[order(dataframe$my_letters),]
print(dataframe)
##      my_unis my_letters
## 17 5.3039563          A
## 4         NA          B
## 9  2.8757399          C
## 8  1.8002406          D
## 18 1.3633687          E
## 20        NA          F
## 3  3.7422295          G
## 12 6.3608842          H
## 21 1.0744556          I
## 23 7.3759481          J
## 14 5.2583477          K
## 25 1.4671458          L
## 6  5.8375806          M
## 26 8.4052260          N
## 15 2.4927578          O
## 24        NA          P
## 2  7.3600030          Q
## 16 7.4304421          R
## 13 9.4769303          S
## 1  6.2961703          T
## 11        NA          U
## 5  0.5344522          V
## 22 6.5077801          W
## 7  2.6416661          X
## 10 6.0383798          Y
## 19 2.4028119          Z

Compute the Mean of the Numeric Column

# Calculate the mean of the numeric column, ignoring NA values
mean(dataframe$my_unis, na.rm=TRUE)
## [1] 4.638478