n_dims
a single random integer
between 3 and 10.n_dims <- floor(runif(1, min = 3, max = 10))
Create a vector of consecutive integers from 1 to n_dims2.
vec <- c(seq(from = 1, to = n_dims^2))
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Use the sample function to randomly reshuffle these values.
shuffle <- sample(vec)
[1] 15 14 2 4 12 10 5 13 16 9 7 3 11 6 1 8
create a square matrix with these elements.
m <- matrix(data = shuffle, nrow = n_dims)
print out the matrix.
[,1] [,2] [,3] [,4]
[1,] 15 12 16 11
[2,] 14 10 9 6
[3,] 2 5 7 1
[4,] 4 13 3 8
find a function in r to transpose the matrix.
trans_m <- t(m)
print it out again and note how it has changed.
[,1] [,2] [,3] [,4]
[1,] 15 14 2 4
[2,] 12 10 5 13
[3,] 16 9 7 3
[4,] 11 6 1 8
calculate the sum and the mean of the elements in the first row and then the last row.
sum(trans_m[1, ])
# 35
mean(trans_m[1, ])
# 8.75
sum(trans_m[n_dims, ])
# 26
mean(trans_m[n_dims, ])
# 6.5
read about the eigen()
function and use it on your
matrix
eigen <- eigen(trans_m)
eigen() decomposition
$values
[1] 34.5682268+0.000000i 5.6782017+0.000000i -0.1232143+4.961585i -0.1232143-4.961585i
$vectors
[,1] [,2] [,3] [,4]
[1,] -0.5272984+0i -0.01191852+0i -0.45002590-0.2960494i -0.45002590+0.2960494i
[2,] -0.5576065+0i 0.03929094+0i 0.61085333+0.0000000i 0.61085333+0.0000000i
[3,] -0.5276925+0i -0.93324716+0i -0.07834909+0.4738877i -0.07834909-0.4738877i
[4,] -0.3641046+0i 0.35688081+0i -0.03013407+0.3241504i -0.03013407-0.3241504i
dig in with the typeof()
function to figure out
their type.
typeof(eigen$values)
# complex
typeof(eigen$vectors)
# complex
my_matrix
, which is a 4 x 4 matrix filled with
random uniform values
my_matrix <- matrix(runif(16), nrow = 4, ncol = 4)
[,1] [,2] [,3] [,4]
[1,] 0.9140789 0.75348962 0.09587903 0.7867439
[2,] 0.5350972 0.90350731 0.94958696 0.2010268
[3,] 0.2468937 0.65154816 0.88817659 0.3127032
[4,] 0.9223468 0.02077916 0.45623399 0.5297618
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) > 0.5
[1] FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE
[17] FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE TRUE
[33] TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE
[49] FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE
[65] FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE TRUE TRUE TRUE
[81] TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE TRUE TRUE FALSE TRUE FALSE TRUE TRUE FALSE
[97] FALSE FALSE FALSE TRUE
my_letters
, which is a 26-element vector of all the
lower-case letters in random order.
my_letters <- sample(letters)
[1] "v" "e" "b" "d" "f" "p" "k" "h" "l" "x" "w" "o" "u" "c" "j" "q" "g" "y" "r" "t" "i" "z" "m" "n" "a" "s"
Create list:
my_list <- list(my_matrix, my_logical, my_letters)
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_list2 <- list(my_matrix[2,2], my_logical[2], my_letters[2])
[[1]]
[1] 0.9035073
[[2]]
[1] TRUE
[[3]]
[1] "e"
use the typeof()
function to confirm the underlying
data types of each component in this list
typeof(my_list2[[1]])
# double
typeof(my_list2[[2]])
# logical
typeof(my_list2[[3]])
# character
combine the underlying elements from the new list into a single
atomic vector with the c()
function.
my_vec <- c(my_list2[[1]], my_list2[[2]], my_list2[[3]])
what is the data type of this vector?
typeof(my_vec)
[1] "character"
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.
my_data <- data.frame(
my_unis = runif(26, min = 0, max = 10),
my_letters = sample(LETTERS))
head(my_data)
my_unis my_letters
1 3.671755 O
2 5.569217 S
3 8.970902 J
4 2.722306 U
5 8.263332 R
6 6.759957 V
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
.
my_data$my_unis[sample(1:26, 4)] <- NA
for the first variable, write a single line of R code to identify which rows have the missing values.
which(is.na(my_data$my_unis))
[1] 6 10 20 23
re-order the entire data frame to arrange the second variable in alphabetical order
my_data$my_letters <- sort(my_data$my_letters)
my_unis my_letters
1 3.671755 A
2 5.569217 B
3 8.970902 C
4 2.722306 D
5 8.263332 E
6 NA F
calculate the column mean for the first variable.
mean(my_data$my_unis, na.rm = TRUE)
[1] 5.866579