#Homewrok5
Nicolás Zapata
Assign to the variable n_dims a single random integer between 3 and 10.
n_dims <- sample(3:10, size=1)
print(n_dims)
## [1] 5
Create a vector of consecutive integers from 1 to n_dims2
my_vector <- seq(1:n_dims^2)
Use the sample function to randomly reshuffle these values.
matrix1 <- matrix(sample(my_vector), nrow=n_dims)
# print out the matrix
print(matrix1)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3 11 20 2 15
## [2,] 17 7 4 23 16
## [3,] 6 14 24 8 19
## [4,] 22 12 21 13 1
## [5,] 10 25 5 9 18
Find a function in r to transpose the matrix
transposed_matrix1 <- t(matrix1)
print(transposed_matrix1)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3 17 6 22 10
## [2,] 11 7 14 12 25
## [3,] 20 4 24 21 5
## [4,] 2 23 8 13 9
## [5,] 15 16 19 1 18
Calculate the sum and the mean of the elements in the first row and then the last row.
# mean first row
firstrow_mean <- mean(matrix1[1,])
print(firstrow_mean)
## [1] 10.2
# sum first row
firstrow_sum <- sum(matrix1[1,])
print(firstrow_sum)
## [1] 51
# mean last row
lastrow_mean <- mean(matrix1[n_dims,])
print(lastrow_mean)
## [1] 13.4
# sum last row
lastrow_sum <- sum(matrix1[n_dims,])
print(lastrow_sum)
## [1] 67
Read about the eigen() function and use it on your matrix
matrix_eigen <- eigen(matrix1)
print(matrix_eigen)
## eigen() decomposition
## $values
## [1] 65.416127+ 0.00000i 5.475014+14.13193i 5.475014-14.13193i
## [4] -13.825774+ 0.00000i 2.459620+ 0.00000i
##
## $vectors
## [,1] [,2] [,3] [,4]
## [1,] 0.3624871+0i -0.1843188+0.3478123i -0.1843188-0.3478123i 0.07133107+0i
## [2,] 0.4460746+0i 0.2068105-0.3161763i 0.2068105+0.3161763i -0.79188304+0i
## [3,] 0.5003184+0i -0.1312502+0.2514007i -0.1312502-0.2514007i -0.04455633+0i
## [4,] 0.4633429+0i 0.6310226+0.0000000i 0.6310226+0.0000000i 0.31128238+0i
## [5,] 0.4523443+0i -0.4188939-0.2196010i -0.4188939+0.2196010i 0.51860499+0i
## [,5]
## [1,] -0.5603989+0i
## [2,] 0.2333287+0i
## [3,] 0.1502010+0i
## [4,] 0.6462734+0i
## [5,] -0.4373570+0i
Dig in with the typeof() function to figure out their type.
typeof(matrix_eigen$values)
## [1] "complex"
typeof(matrix_eigen$vectors)
## [1] "complex"
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(data=runif(16), nrow=4)
print(my_matrix)
## [,1] [,2] [,3] [,4]
## [1,] 0.8594640 0.7566499 0.6812965 0.3887482
## [2,] 0.5842931 0.6617238 0.6269719 0.6380629
## [3,] 0.8344392 0.7072062 0.9743014 0.4090533
## [4,] 0.6052404 0.3750682 0.1993862 0.4773114
# 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_logical <- (runif(100) > .8)
print(my_logical)
## [1] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [25] FALSE TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE
## [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE
## [49] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
## [61] TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
## [73] TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
## [85] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [97] FALSE FALSE FALSE FALSE
# my_letters, which is a 26-element vector of all the lower-case letters in random order.
my_letters <- sample(letters)
print(my_letters)
## [1] "r" "g" "q" "j" "c" "p" "i" "b" "n" "d" "y" "w" "z" "t" "v" "a" "k" "u" "x"
## [20] "o" "f" "m" "s" "e" "l" "h"
# 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
my_list1 <- list(my_matrix, my_logical, my_letters)
new_list <- list(my_matrix[2,2], my_logical [2], my_letters[2])
print(new_list)
## [[1]]
## [1] 0.6617238
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] "g"
# use the typeof() function to confirm the underlying data types of each component in this list
typeof(new_list[[1]])
## [1] "double"
typeof(new_list[[2]])
## [1] "logical"
typeof(new_list[[3]])
## [1] "character"
# combine the underlying elements from the new list into a single atomic vector with the c() function
combined_elements_vector <- c(new_list[[1]], new_list[[2]], new_list[[3]])
print(combined_elements_vector)
## [1] "0.661723831901327" "TRUE" "g"
# what is the data type of this vector?
typeof(combined_elements_vector)
## [1] "character"
Create a data frame with the two variables (= columns) and 26 cases (= rows) below:
#call the first variable my_unis and fill it with 26 random uniform values from 0 to 10
# call the second variable my_letters and fill it with 26 capital letters in random order.
dataframe <- data.frame(my_unis = runif(26, min=0, max=10), my_letters = sample(LETTERS))
print(dataframe)
## my_unis my_letters
## 1 8.30943082 Q
## 2 0.88950208 B
## 3 1.17543635 Y
## 4 1.47413393 J
## 5 0.05714084 C
## 6 3.05226702 I
## 7 3.53905776 T
## 8 2.42493663 V
## 9 6.27864312 U
## 10 0.60433896 Z
## 11 0.69836300 D
## 12 0.75796501 X
## 13 3.87724149 A
## 14 2.91420274 L
## 15 2.17319479 S
## 16 6.01984606 G
## 17 8.29146163 N
## 18 4.71310582 W
## 19 7.55643550 H
## 20 0.77786305 M
## 21 3.86182401 E
## 22 4.99765874 P
## 23 6.60152977 R
## 24 5.51694524 O
## 25 0.61585670 K
## 26 0.77373513 F
# 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.
dataframe[sample(1:26, size=4), 1] <- NA
# for the first variable, write a single line of R code to identify which rows have the missing values
which(is.na(dataframe$my_unis))
## [1] 4 13 15 24
# re-order the entire data frame to arrange the second variable in alphabetical order
dataframe <- dataframe[order(dataframe$my_letters),]
print(dataframe)
## my_unis my_letters
## 13 NA A
## 2 0.88950208 B
## 5 0.05714084 C
## 11 0.69836300 D
## 21 3.86182401 E
## 26 0.77373513 F
## 16 6.01984606 G
## 19 7.55643550 H
## 6 3.05226702 I
## 4 NA J
## 25 0.61585670 K
## 14 2.91420274 L
## 20 0.77786305 M
## 17 8.29146163 N
## 24 NA O
## 22 4.99765874 P
## 1 8.30943082 Q
## 23 6.60152977 R
## 15 NA S
## 7 3.53905776 T
## 9 6.27864312 U
## 8 2.42493663 V
## 18 4.71310582 W
## 12 0.75796501 X
## 3 1.17543635 Y
## 10 0.60433896 Z
# calculate the column mean for the first variable
mean(dataframe$my_unis, na.rm=TRUE)
## [1] 3.405027