integer <- 3:10
n_dims <- sample(x=integer, size=1)
print(n_dims) # 6
vector <- (1:n_dims^2) # 1:36
vector_shuffle <- sample(x=vector, size=n_dims^2)
matrix <- matrix(data=vector_shuffle,nrow=n_dims)
print(matrix)
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 1 21 23 25 34 29
# [2,] 35 10 15 7 20 32
# [3,] 28 33 31 17 27 5
# [4,] 26 8 13 12 11 14
# [5,] 30 18 6 2 3 19
# [6,] 9 22 24 4 36 16
matrix_transposed <- t(matrix) # transpose matrix
print(matrix_transposed)
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 1 35 28 26 30 9
# [2,] 21 10 33 8 18 22
# [3,] 23 15 31 13 6 24
# [4,] 25 7 17 12 2 4
# [5,] 34 20 27 11 3 36
# [6,] 29 32 5 14 19 16
rowSums(matrix_transposed)
# row sum first row: 129
# row sum last row: 115
rowMeans(matrix_transposed)
# mean first row: 21.50000
# mean last row: 19.16667
eigen <- eigen(matrix_transposed)
print(eigen)
#eigenvalues:
# $values
# [1] 112.302019+0.00000i -30.249797+0.00000i
# [3] -11.769307+8.35719i -11.769307-8.35719i
# [5] 13.643605+0.00000i 0.842788+0.00000i
#eigenvectors:
# $vectors
# [,1] [,2] [,3]
# [1,] -0.4499653+0i -0.79766792+0i -0.3793917+0.1303107i
# [2,] -0.4155692+0i -0.03582203+0i -0.3513038+0.2250913i
# [3,] -0.4018432+0i 0.09212071+0i -0.0946119-0.1420926i
# [4,] -0.2356731+0i 0.40048837+0i 0.4426958+0.0845954i
# [5,] -0.4783404+0i 0.36668616+0i 0.0770189-0.3644569i
# [6,] -0.4230889+0i 0.24311883+0i 0.5421828+0.0000000i
# [,4] [,5] [,6]
# [1,] -0.3793917-0.1303107i 0.1337994+0i -0.01757728+0i
# [2,] -0.3513038-0.2250913i -0.2778675+0i -0.50057467+0i
# [3,] -0.0946119+0.1420926i 0.1248984+0i -0.33103958+0i
# [4,] 0.4426958-0.0845954i 0.7970232+0i 0.71267464+0i
# [5,] 0.0770189+0.3644569i -0.3067843+0i 0.18110577+0i
# [6,] 0.5421828+0.0000000i -0.3999065+0i 0.31436347+0i
typeof(eigen$values)
# [1] "complex"
typeof(eigen$vectors)
# [1] "complex"
#code created such that can be re-run and result in matrix of different size
# create my_matrix, my_logical, and my_letters
data <- runif(16)
my_matrix <- matrix(data=data, nrow=4)
print(my_matrix)
# [,1] [,2] [,3] [,4]
# [1,] 0.2640085 0.8648052 0.7115569 0.99095967
# [2,] 0.1973545 0.1984534 0.4480728 0.79441447
# [3,] 0.9309638 0.6077629 0.7884239 0.08921485
# [4,] 0.5954754 0.9232030 0.9716622 0.85597051
big_vector <- runif(100)
my_logical <- big_vector < 0.5
print(my_logical)
# [1] FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE
# [9] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
# [17] TRUE FALSE TRUE TRUE FALSE TRUE TRUE FALSE
# [25] FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE
# [33] TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE
# [41] FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE
# [49] TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE
# [57] FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE
# [65] TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE
# [73] TRUE TRUE TRUE FALSE TRUE TRUE TRUE FALSE
# [81] FALSE FALSE TRUE FALSE FALSE TRUE TRUE TRUE
# [89] FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE
# [97] TRUE TRUE FALSE FALSE
my_letters <- sample(x=letters,size=26)
print(my_letters)
# [1] "s" "c" "d" "o" "y" "j" "x" "t" "p" "z" "f" "n" "u"
# [14] "w" "v" "r" "k" "i" "l" "q" "h" "e" "g" "b" "a" "m"
# create the list of my_matrix, my_logical, and my_letters
my_list <- list(my_matrix, my_logical, my_letters)
# create new list with element [2,2] of matrix, 2nd element of logical vector, and 2nd element of letters vector
new_list <- list(my_matrix[2,2], my_logical[2], my_letters[2])
print(new_list)
# [[1]]
# [1] 0.1984534
#
# [[2]]
# [1] FALSE
#
# [[3]]
# [1] "c"
#find type for each list component
typeof(new_list[[1]]) # double
typeof(new_list[[2]]) # logical
typeof(new_list[[3]]) # character
# combine new_list elements into single atomic vector
single_vector <- c(new_list[[1]], new_list[[2]], new_list[[3]])
#find data type for single_vector
typeof(single_vector) # coerced to character type
# create data frame
my_unis <- runif(26,min=0,max=10)
my_letters <- sample(x=LETTERS,size=26)
data_frame <- data.frame(my_unis, my_letters)
# replace numerical values with NA in 4 random rows
data_frame[sample(x=my_unis,size=4),1] <- NA
# find rows with the missing values
which(!complete.cases(data_frame))
# [1] 2 4 5 8
# sort data_frame so 2nd variable is in alphabetical order
sorted_data_frame <- data_frame[order(my_letters), ]
#mean of column 1, with NAs removed from the calculation
mean(data_frame$my_unis, na.rm=TRUE)
[1] 4.79679