#1
#Assign a single random integer between 3 and 10 n_dims
set.seed(Sys.time()) #Ensure different values for each run
n_dims <- sample (3:10, 1)
#Create a vector of consecutive integers from 1 to n_dims^2
vec <- 1:(n_dims^2)
#Randomly shuffle these values
shuffled_vec <- sample(vec)
#create a square matrix
matrix_obj <- matrix(shuffled_vec, nrow = n_dims, ncol = n_dims)
#Print the original matrix
print("Original Matrix:")
## [1] "Original Matrix:"
print(matrix_obj)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 53 84 91 72 26 58 10 79 21 82
## [2,] 51 22 85 66 96 77 54 62 35 27
## [3,] 1 32 65 99 50 98 69 7 83 17
## [4,] 60 6 75 59 56 11 95 13 43 68
## [5,] 45 48 71 73 70 67 63 97 33 49
## [6,] 31 44 47 61 52 3 92 34 57 9
## [7,] 78 20 93 55 64 80 76 39 94 38
## [8,] 90 88 46 24 89 41 74 30 23 25
## [9,] 28 18 2 37 42 86 15 19 81 87
## [10,] 12 8 5 40 14 4 36 100 16 29
#Transpose the matrix using the t() function
transposed_matrix <- t(matrix_obj)
#Print the transposed matrix
print("Transposed Matrix:")
## [1] "Transposed Matrix:"
print(transposed_matrix)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 53 51 1 60 45 31 78 90 28 12
## [2,] 84 22 32 6 48 44 20 88 18 8
## [3,] 91 85 65 75 71 47 93 46 2 5
## [4,] 72 66 99 59 73 61 55 24 37 40
## [5,] 26 96 50 56 70 52 64 89 42 14
## [6,] 58 77 98 11 67 3 80 41 86 4
## [7,] 10 54 69 95 63 92 76 74 15 36
## [8,] 79 62 7 13 97 34 39 30 19 100
## [9,] 21 35 83 43 33 57 94 23 81 16
## [10,] 82 27 17 68 49 9 38 25 87 29
#Calculate sum and mean of the first and last rows
first_row_sum <- sum(matrix_obj[1, ])
first_row_mean <- mean(matrix_obj[1, ])
last_row_sum <- sum(matrix_obj[n_dims, ])
last_row_mean <- mean(matrix_obj[n_dims, ])
print(paste("First row sum:", first_row_sum))
## [1] "First row sum: 576"
print(paste("First row mean:", first_row_mean))
## [1] "First row mean: 57.6"
print(paste("Last row sum:", last_row_sum))
## [1] "Last row sum: 264"
print(paste("Last row mean:", last_row_mean))
## [1] "Last row mean: 26.4"
#Use the eigen() function on the matrix
eigen_result <- eigen(matrix_obj)
#Print eigen values and eigen vectors
print("Eigenvalues:")
## [1] "Eigenvalues:"
print(eigen_result$values)
## [1] 508.45098+ 0.000000i 80.77336+60.712652i 80.77336-60.712652i
## [4] -57.50200+41.766916i -57.50200-41.766916i -8.94894+42.823137i
## [7] -8.94894-42.823137i -37.86636+ 0.632786i -37.86636- 0.632786i
## [10] 26.63690+ 0.000000i
print("Eigenvectors:")
## [1] "Eigenvectors:"
print(eigen_result$vectors)
## [,1] [,2] [,3]
## [1,] 0.3413117+0i -0.20744462-0.18523707i -0.20744462+0.18523707i
## [2,] 0.3617617+0i -0.01799875-0.19700909i -0.01799875+0.19700909i
## [3,] 0.3101070+0i 0.50966331+0.00000000i 0.50966331+0.00000000i
## [4,] 0.2941777+0i 0.14300354+0.02712108i 0.14300354-0.02712108i
## [5,] 0.3837715+0i -0.18866354-0.13872649i -0.18866354+0.13872649i
## [6,] 0.2775306+0i 0.12575341-0.01542373i 0.12575341+0.01542373i
## [7,] 0.3844735+0i 0.19256952+0.06611826i 0.19256952-0.06611826i
## [8,] 0.3491720+0i -0.29955796-0.20473949i -0.29955796+0.20473949i
## [9,] 0.2206337+0i -0.16692937+0.46497588i -0.16692937-0.46497588i
## [10,] 0.1649361+0i -0.32710325+0.08667177i -0.32710325-0.08667177i
## [,4] [,5] [,6]
## [1,] -0.158129668-0.2008096i -0.158129668+0.2008096i 0.21051879-0.357375088i
## [2,] 0.001049769-0.1451343i 0.001049769+0.1451343i 0.12330666+0.005274917i
## [3,] 0.223378536-0.1125767i 0.223378536+0.1125767i 0.02576378+0.048447596i
## [4,] -0.313943861+0.0882317i -0.313943861-0.0882317i 0.22707714-0.010192179i
## [5,] 0.181433679-0.1731017i 0.181433679+0.1731017i 0.18676922+0.236597650i
## [6,] 0.161747447+0.4119141i 0.161747447-0.4119141i -0.28341610+0.034192495i
## [7,] -0.065556799+0.1284366i -0.065556799-0.1284366i -0.47673279+0.000000000i
## [8,] -0.255480861+0.2122759i -0.255480861-0.2122759i -0.17925675-0.111693142i
## [9,] -0.219728986-0.2750564i -0.219728986+0.2750564i 0.26952695-0.272467230i
## [10,] 0.480529390+0.0000000i 0.480529390+0.0000000i -0.02364187+0.407889782i
## [,7] [,8]
## [1,] 0.21051879+0.357375088i 0.21062555+0.028976311i
## [2,] 0.12330666-0.005274917i -0.14900241-0.014190633i
## [3,] 0.02576378-0.048447596i -0.43314358-0.019190424i
## [4,] 0.22707714+0.010192179i 0.24833197+0.030708740i
## [5,] 0.18676922-0.236597650i -0.17247744-0.006113256i
## [6,] -0.28341610-0.034192495i 0.59585499+0.000000000i
## [7,] -0.47673279+0.000000000i 0.22586695-0.011541427i
## [8,] -0.17925675+0.111693142i -0.09861487+0.010360980i
## [9,] 0.26952695+0.272467230i -0.48474728+0.007561352i
## [10,] -0.02364187-0.407889782i 0.00648384-0.030186896i
## [,9] [,10]
## [1,] 0.21062555-0.028976311i 0.5500473+0i
## [2,] -0.14900241+0.014190633i -0.1729891+0i
## [3,] -0.43314358+0.019190424i -0.1784506+0i
## [4,] 0.24833197-0.030708740i 0.6784956+0i
## [5,] -0.17247744+0.006113256i -0.2067954+0i
## [6,] 0.59585499+0.000000000i -0.1368922+0i
## [7,] 0.22586695+0.011541427i -0.1767517+0i
## [8,] -0.09861487-0.010360980i -0.1814909+0i
## [9,] -0.48474728-0.007561352i -0.2220414+0i
## [10,] 0.00648384+0.030186896i 0.0183177+0i
#Check the type of eigen values and eigen vectors
print(paste("Type of eigenvalues:", typeof(eigen_result$values)))
## [1] "Type of eigenvalues: complex"
print(paste("Type of eigenvectors:", typeof(eigen_result$vectors)))
## [1] "Type of eigenvectors: complex"
#2
#Create a 4x4 matrix filled with random uniform values
set.seed(123) #For reproducibility
my_matrix <- matrix(runif(16), nrow=4, ncol=4)
#Create a 100-element logival vector (TRUE/FALSE)
my_logical <- runif(100) > 0.5 #Generates TRUE if value >0.5 , FALSE otherwise
#Create a 26-element vector of lowercase letters in random order
my_letters <- sample(letters)
#Create a list with these named elements
my_list <- list(
my_matrix = my_matrix,
my_logical = my_logical,
my_letters = my_letters
)
#Create a new list with specific elements
new_list <- list (
matrix_element = my_list$my_matrix[2, 2], #element [2,2] from matrix
logical_element= my_list$my_logical[2], #second element of logical vector
letter_element = my_list$my_letters[2] #second element of letter vector
)
#Print the new list
print("New List:")
## [1] "New List:"
print(new_list)
## $matrix_element
## [1] 0.0455565
##
## $logical_element
## [1] FALSE
##
## $letter_element
## [1] "n"
#Check data types using typeof()
print("Data types of new list components:")
## [1] "Data types of new list components:"
print(typeof(new_list$matrix_element)) # type of matrix element
## [1] "double"
print(typeof(new_list$logical_element)) #type of logical element
## [1] "logical"
print(typeof(new_list$letter_element)) #type of letter element
## [1] "character"
#Combine the elements into a single atomic vector
atomic_vector <- c(new_list$matrix_element, new_list$logical_element, new_list$letter_element)
#Print the atomic vector
print("Combined Atomic Vector:")
## [1] "Combined Atomic Vector:"
print(atomic_vector)
## [1] "0.0455564993899316" "FALSE" "n"
#Check data type of the combined atomic vector
print("Data type of combined atomic vector:")
## [1] "Data type of combined atomic vector:"
print(typeof(atomic_vector))
## [1] "character"
#3
#Set seed for reproducibility
set.seed(123)
#Create a data framw with two variables
my_data <-data.frame(
my_unis = runif(26, min = 0, max = 10), #26 random uniform values
my_letters = sample(LETTERS) #26 capital letters in random order
)
#Print the original data frame
print("Original Data Frame:")
## [1] "Original Data Frame:"
print(my_data)
## my_unis my_letters
## 1 2.8757752 H
## 2 7.8830514 G
## 3 4.0897692 J
## 4 8.8301740 I
## 5 9.4046728 S
## 6 0.4555650 D
## 7 5.2810549 N
## 8 8.9241904 Q
## 9 5.5143501 K
## 10 4.5661474 Y
## 11 9.5683335 E
## 12 4.5333416 L
## 13 6.7757064 X
## 14 5.7263340 M
## 15 1.0292468 V
## 16 8.9982497 W
## 17 2.4608773 R
## 18 0.4205953 O
## 19 3.2792072 C
## 20 9.5450365 U
## 21 8.8953932 P
## 22 6.9280341 F
## 23 6.4050681 Z
## 24 9.9426978 B
## 25 6.5570580 A
## 26 7.0853047 T
#Replace 4 random numbers in 'my_unis' column with NA
my_data$my_unis[sample(1:26, 4)] <- NA
#Print the modified data frame
print("Data Frame with NA values:")
## [1] "Data Frame with NA values:"
print(my_data)
## my_unis my_letters
## 1 2.8757752 H
## 2 7.8830514 G
## 3 4.0897692 J
## 4 8.8301740 I
## 5 NA S
## 6 0.4555650 D
## 7 5.2810549 N
## 8 NA Q
## 9 5.5143501 K
## 10 4.5661474 Y
## 11 9.5683335 E
## 12 NA L
## 13 NA X
## 14 5.7263340 M
## 15 1.0292468 V
## 16 8.9982497 W
## 17 2.4608773 R
## 18 0.4205953 O
## 19 3.2792072 C
## 20 9.5450365 U
## 21 8.8953932 P
## 22 6.9280341 F
## 23 6.4050681 Z
## 24 9.9426978 B
## 25 6.5570580 A
## 26 7.0853047 T
#Identify which rows have missing values in 'my_unis'
na_rows <- which(is.na(my_data$my_unis))
print("Rows with missing values:")
## [1] "Rows with missing values:"
print(na_rows)
## [1] 5 8 12 13
#Reorder the data frames by 'my_letters' in alphabetical order
my_data <- my_data[order(my_data$my_letters), ]
#Print the sorted data frame
print("Reordered Data Frame:")
## [1] "Reordered Data Frame:"
print(my_data)
## my_unis my_letters
## 25 6.5570580 A
## 24 9.9426978 B
## 19 3.2792072 C
## 6 0.4555650 D
## 11 9.5683335 E
## 22 6.9280341 F
## 2 7.8830514 G
## 1 2.8757752 H
## 4 8.8301740 I
## 3 4.0897692 J
## 9 5.5143501 K
## 12 NA L
## 14 5.7263340 M
## 7 5.2810549 N
## 18 0.4205953 O
## 21 8.8953932 P
## 8 NA Q
## 17 2.4608773 R
## 5 NA S
## 26 7.0853047 T
## 20 9.5450365 U
## 15 1.0292468 V
## 16 8.9982497 W
## 13 NA X
## 10 4.5661474 Y
## 23 6.4050681 Z
#Calculate the mean of 'my_unis' column, ignoring NA values
column_mean <- mean(my_data$my_unis, na.rm = TRUE)
print(paste("Mean of my_unis (excluding NAs):", column_mean))
## [1] "Mean of my_unis (excluding NAs): 5.74260560689833"