Assign to the variable n_dims
a single random integer between 3 and 10.
n_dims <- sample(3:10, 1)
print(n_dims)
## [1] 3
Create a vector of consecutive integers from 1 to n_dims^2.
my_vec <- 1:n_dims^2
print(my_vec)
## [1] 1 2 3 4 5 6 7 8 9
Use the sample function to randomly reshuffle these values.
my_vec2 <- sample(my_vec)
print(my_vec2)
## [1] 4 2 8 5 3 7 9 1 6
create a square matrix with these elements.
m <- matrix(data=my_vec2,nrow=n_dims)
print out the matrix.
print(m)
## [,1] [,2] [,3]
## [1,] 4 5 9
## [2,] 2 3 1
## [3,] 8 7 6
find a function in r to transpose the matrix.
m_transposed <- t(m)
print it out again and note how it has changed.
print(m_transposed)
## [,1] [,2] [,3]
## [1,] 4 2 8
## [2,] 5 3 7
## [3,] 9 1 6
calculate the sum and the mean of the elements in the first row and the last row.
# Sum of elements in the first row
firstrow_sum = sum(m_transposed[1,])
print(firstrow_sum)
## [1] 14
# Mean of elements in the first row
firstrow_mean = mean(m_transposed[1,])
print(firstrow_mean)
## [1] 4.666667
# Sum of elements in the last row
lastrow_sum = sum(m_transposed[n_dims,])
print(lastrow_sum)
## [1] 16
# Mean of elements in the last row
lastrow_mean = mean(m_transposed[n_dims,])
print(lastrow_mean)
## [1] 5.333333
read about the eigen()
function and use it on your matrix
m_eigen <- eigen(m_transposed)
print(m_eigen)
## eigen() decomposition
## $values
## [1] 15.035835 -3.347224 1.311389
##
## $vectors
## [,1] [,2] [,3]
## [1,] -0.5446869 -0.6919100 -0.02244808
## [2,] -0.5790817 -0.2150462 -0.96809692
## [3,] -0.6066140 0.6892138 0.24956850
look carefully at the elements of $values
and $vectors
. What kind of numbers are these?
print(m_eigen$values)
## [1] 15.035835 -3.347224 1.311389
The $values are non-integers, and can be negative or positive.
print(m_eigen$vectors)
## [,1] [,2] [,3]
## [1,] -0.5446869 -0.6919100 -0.02244808
## [2,] -0.5790817 -0.2150462 -0.96809692
## [3,] -0.6066140 0.6892138 0.24956850
The $vectors are non-integers and all fall between -1 and 1. Some are negative and some are positive.
dig in with the typeof()
function to figure out their type.
typeof(m_eigen$values)
## [1] "double"
typeof(m_eigen$vectors)
## [1] "double"
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_vec <- runif(16)
my_matrix <- matrix(data=my_vec, nrow=4)
print(my_matrix)
## [,1] [,2] [,3] [,4]
## [1,] 0.5768577 0.5832659 0.88830189 0.1029386
## [2,] 0.9513893 0.9061931 0.07700176 0.2721883
## [3,] 0.2010140 0.2688004 0.24922334 0.3390551
## [4,] 0.7767917 0.9076689 0.50624454 0.5599691
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_vec2 <- runif(100)
my_logical <- my_vec2 > 0.5
print(my_logical)
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE
## [13] FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE
## [25] TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE TRUE TRUE TRUE
## [37] TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE
## [49] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [61] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE FALSE FALSE TRUE
## [73] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE TRUE TRUE FALSE FALSE
## [85] TRUE TRUE FALSE FALSE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE
## [97] FALSE FALSE FALSE TRUE
my_letters
, which is a 26-element vector of all the lower-case letters in random order.
my_letters_ordered <- letters[1:26]
my_letters <- sample(my_letters_ordered)
print(my_letters)
## [1] "g" "i" "x" "y" "a" "c" "j" "p" "w" "h" "d" "k" "b" "o" "v" "t" "f" "l" "r"
## [20] "q" "e" "u" "s" "z" "n" "m"
create a list with these
my_list <- list(my_matrix, my_logical, my_letters)
print(my_list)
## [[1]]
## [,1] [,2] [,3] [,4]
## [1,] 0.5768577 0.5832659 0.88830189 0.1029386
## [2,] 0.9513893 0.9061931 0.07700176 0.2721883
## [3,] 0.2010140 0.2688004 0.24922334 0.3390551
## [4,] 0.7767917 0.9076689 0.50624454 0.5599691
##
## [[2]]
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE
## [13] FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE
## [25] TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE TRUE TRUE TRUE
## [37] TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE
## [49] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [61] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE FALSE FALSE TRUE
## [73] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE TRUE TRUE FALSE FALSE
## [85] TRUE TRUE FALSE FALSE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE
## [97] FALSE FALSE FALSE TRUE
##
## [[3]]
## [1] "g" "i" "x" "y" "a" "c" "j" "p" "w" "h" "d" "k" "b" "o" "v" "t" "f" "l" "r"
## [20] "q" "e" "u" "s" "z" "n" "m"
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_list_2 <- list(my_matrix[2,2], my_logical[2], my_letters[2])
print(my_list_2)
## [[1]]
## [1] 0.9061931
##
## [[2]]
## [1] FALSE
##
## [[3]]
## [1] "i"
use the typeof()
function to confirm the underlying data types of each component in this list
typeof(my_list_2[[1]])
## [1] "double"
typeof(my_list_2[[2]])
## [1] "logical"
typeof(my_list_2[[3]])
## [1] "character"
combine the underlying elements from the new list into a single atomic vector with the c()
function.
my_vector <- c(my_list_2[[1]], my_list_2[[2]], my_list_2[[3]])
print(my_vector)
## [1] "0.906193064758554" "FALSE" "i"
what is the data type of this vector?
print(typeof(my_vector))
## [1] "character"
Create a data frame with two variables (= columns) and 26 cases (=rows).
call the first variable my_unis
and fill it with 26 random uniform values from 0 to 10
my_unis <- runif(26, min=0, max=10)
print(my_unis)
## [1] 2.7446878 5.9707810 2.8345759 2.4492838 0.1244673 1.7170151 2.8127937
## [8] 3.2041870 3.4377870 8.4812927 3.5787139 3.4544960 4.1441395 1.9487458
## [15] 7.1293836 4.5751757 4.7650505 1.7127797 5.3488301 7.7625121 0.5790367
## [22] 0.4573841 2.1608956 7.9072569 6.9530396 6.7685595
call the second variable my_letters
and fill it with 26 capital letters in random order.
my_letters_ordered <- LETTERS[1:26]
my_letters <- sample(my_letters_ordered)
print(my_letters)
## [1] "M" "E" "N" "Y" "R" "G" "S" "D" "B" "X" "Q" "O" "V" "A" "T" "H" "U" "J" "C"
## [20] "F" "K" "L" "P" "W" "I" "Z"
create the data frame
df <- data.frame(my_unis, my_letters)
print(df)
## my_unis my_letters
## 1 2.7446878 M
## 2 5.9707810 E
## 3 2.8345759 N
## 4 2.4492838 Y
## 5 0.1244673 R
## 6 1.7170151 G
## 7 2.8127937 S
## 8 3.2041870 D
## 9 3.4377870 B
## 10 8.4812927 X
## 11 3.5787139 Q
## 12 3.4544960 O
## 13 4.1441395 V
## 14 1.9487458 A
## 15 7.1293836 T
## 16 4.5751757 H
## 17 4.7650505 U
## 18 1.7127797 J
## 19 5.3488301 C
## 20 7.7625121 F
## 21 0.5790367 K
## 22 0.4573841 L
## 23 2.1608956 P
## 24 7.9072569 W
## 25 6.9530396 I
## 26 6.7685595 Z
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$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(df$my_unis))
## [1] 3 16 18 26
for the second variable, sort it in alphabetical order
df[order(df$my_letters), ]
## my_unis my_letters
## 14 1.9487458 A
## 9 3.4377870 B
## 19 5.3488301 C
## 8 3.2041870 D
## 2 5.9707810 E
## 20 7.7625121 F
## 6 1.7170151 G
## 16 NA H
## 25 6.9530396 I
## 18 NA J
## 21 0.5790367 K
## 22 0.4573841 L
## 1 2.7446878 M
## 3 NA N
## 12 3.4544960 O
## 23 2.1608956 P
## 11 3.5787139 Q
## 5 0.1244673 R
## 7 2.8127937 S
## 15 7.1293836 T
## 17 4.7650505 U
## 13 4.1441395 V
## 24 7.9072569 W
## 10 8.4812927 X
## 4 2.4492838 Y
## 26 NA Z
calculate the column mean for the first variable.
mean(df$my_unis, na.rm = TRUE)
## [1] 3.960535