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