set.seed(7)
# Assign to the variable n_dims a single random integer between 3 and 10.
n_dims <- sample(3:10,1)
# Create a vector of consecutive integers from 1 to n_dims^2.
vec <- 1:(n_dims^2)
# Use the sample function to randomly reshuffle these values.
vec2 <- sample(vec, length(vec), replace=FALSE)
# create a square matrix with these elements.
mat <- matrix(vec2, nrow=n_dims, byrow=TRUE)
# print out the matrix.
print(mat)
## [,1] [,2] [,3] [,4]
## [1,] 3 15 12 7
## [2,] 2 10 6 8
## [3,] 9 16 11 13
## [4,] 14 5 4 1
# find a function in r to transpose the matrix.
mat_t <- t(mat)
# print it out again and note how it has changed.
print(mat_t)
## [,1] [,2] [,3] [,4]
## [1,] 3 2 9 14
## [2,] 15 10 16 5
## [3,] 12 6 11 4
## [4,] 7 8 13 1
# calculate the sum and the mean of the elements in the first row and then the last row.
sum(mat_t[,1])
## [1] 37
mean(mat_t[,1])
## [1] 9.25
sum(mat_t[,n_dims])
## [1] 24
mean(mat_t[,n_dims])
## [1] 6
# read about the eigen() function and use it on your matrix
##?eigen()
mat_t_e <- eigen(mat_t)
# look carefully at the elements of $values and $vectors in the output. What kind of numbers are these?
mat_t_e$values
## [1] 33.214288+0.000000i -4.459531+5.677169i -4.459531-5.677169i
## [4] 0.704774+0.000000i
mat_t_e$vectors
## [,1] [,2] [,3] [,4]
## [1,] -0.3856510+0i 0.7263887+0.0000000i 0.7263887+0.0000000i 0.07464911+0i
## [2,] -0.6650280+0i -0.3336523-0.0462810i -0.3336523+0.0462810i -0.84690318+0i
## [3,] -0.4666863+0i -0.3215908-0.2135864i -0.3215908+0.2135864i 0.48559513+0i
## [4,] -0.4372812+0i -0.1326356+0.4384765i -0.1326356-0.4384765i -0.20342046+0i
## they are imaginary/complex numbers
# dig in with the typeof() function to figure out their type.
typeof(mat_t_e$values)
## [1] "complex"
typeof(mat_t_e$vectors)
## [1] "complex"
# 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.
# 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.
my_matrix <- matrix(runif(4^2), nrow=4, byrow=TRUE)
my_logical <- rpois(100,7) < 7
my_letters <- sample(letters, 26, replace=FALSE)
list1 <- list(my_matrix, my_logical, my_letters)
# 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?
list2 <- list(list1[[1]][2,2], list1[[2]][2], list1[[3]][2])
typeof(list2[[1]])
## [1] "double"
typeof(list2[[2]])
## [1] "logical"
typeof(list2[[3]])
## [1] "character"
vec1 <- c(list2[[1]], list2[[2]], list2[[3]])
typeof(vec1)
## [1] "character"
## it's a character vector
# 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.
# 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.
# for the first variable, write a single line of R code to identify which rows have the missing values.
# re-order the entire data frame to arrange the second variable in alphabetical order
# calculate the column mean for the first variable.
df <- data.frame(my_unis = runif(26,min=0,max=10),
my_letters = sample(LETTERS, 26, replace=FALSE))
df
## my_unis my_letters
## 1 3.6464558 L
## 2 7.3161996 N
## 3 3.8033252 K
## 4 7.1483127 I
## 5 0.3705825 H
## 6 4.9855914 A
## 7 7.2149037 X
## 8 9.7851800 J
## 9 9.4905870 R
## 10 2.9609209 Z
## 11 2.5932639 C
## 12 9.2397398 V
## 13 7.3203924 E
## 14 7.3972332 B
## 15 5.9343637 G
## 16 0.8026144 M
## 17 8.0136332 U
## 18 2.6115888 Q
## 19 2.8310378 T
## 20 7.1415709 Y
## 21 8.6821505 S
## 22 7.0132305 D
## 23 0.6178004 W
## 24 4.3741093 P
## 25 3.3064823 O
## 26 9.0420513 F
df$my_unis[sample(1:26,4,replace=FALSE)] <- NA
df[is.na(df$my_unis),]
## my_unis my_letters
## 2 NA N
## 11 NA C
## 15 NA G
## 23 NA W
df2 <- df[order(df$my_letters),]
df2
## my_unis my_letters
## 6 4.9855914 A
## 14 7.3972332 B
## 11 NA C
## 22 7.0132305 D
## 13 7.3203924 E
## 26 9.0420513 F
## 15 NA G
## 5 0.3705825 H
## 4 7.1483127 I
## 8 9.7851800 J
## 3 3.8033252 K
## 1 3.6464558 L
## 16 0.8026144 M
## 2 NA N
## 25 3.3064823 O
## 24 4.3741093 P
## 18 2.6115888 Q
## 9 9.4905870 R
## 21 8.6821505 S
## 19 2.8310378 T
## 17 8.0136332 U
## 12 9.2397398 V
## 23 NA W
## 7 7.2149037 X
## 20 7.1415709 Y
## 10 2.9609209 Z
mean(df$my_unis, na.rm=TRUE)
## [1] 5.780986