Question 1:
n_dims <- runif(1, 3, 10)
vec <- c(1:(n_dims^2))
samp <- sample(vec)
m <- matrix(samp, nrow = n_dims, ncol = n_dims)
## Warning in matrix(samp, nrow = n_dims, ncol = n_dims): data length [19] is not a
## sub-multiple or multiple of the number of rows [4]
print(m)
## [,1] [,2] [,3] [,4]
## [1,] 18 14 8 9
## [2,] 17 13 7 10
## [3,] 16 1 12 2
## [4,] 4 19 5 6
trans_m <- t(m)
print(trans_m)
## [,1] [,2] [,3] [,4]
## [1,] 18 17 16 4
## [2,] 14 13 1 19
## [3,] 8 7 12 5
## [4,] 9 10 2 6
sum(trans_m[1,])
## [1] 55
mean(trans_m[1,])
## [1] 13.75
sum(trans_m[n_dims,])
## [1] 27
mean(trans_m[n_dims,])
## [1] 6.75
eigen <- eigen(trans_m)
typeof(eigen)
## [1] "list"
Question 2:
r_num <- runif(16)
my_matrix <- matrix(r_num, 4, 4)
r_num2 <- runif(100)
my_logical <- r_num2<50
my_letters <- sample(letters, length(letters))
print(my_letters)
## [1] "l" "c" "o" "s" "p" "e" "y" "d" "f" "g" "j" "m" "b" "v" "t" "z" "k" "r" "x"
## [20] "n" "q" "a" "w" "u" "h" "i"
my_list <- list(my_matrix,my_logical, my_letters)
my_list2 <- list(my_matrix[2,2], my_logical[2], my_letters[2])
sapply(my_list2, typeof)
## [1] "double" "logical" "character"
vec2 <- c(my_matrix[2,2], my_logical[2], my_letters[2])
typeof(vec2)
## [1] "character"
Question 3:
my_unis <- runif(26, 0, 10)
my_letters <- sample(LETTERS, length(LETTERS))
df <- data.frame(my_unis,my_letters)
#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.
df[sample(nrow(df), 4), "my_unis"] <- NA
#for the first variable, write a single line of R code to identify which rows have the missing values.
which(!complete.cases(df$my_unis))
## [1] 4 16 22 25
#re-order the entire data frame to arrange the second variable in alphabetical order
df <- df[order(df$my_letters), ]
#calculate the column mean for the first variable
mean(df$my_unis, na.rm = T)
## [1] 4.713926