n_dims <- sample(3:10, 1, replace = TRUE)
print(n_dims)
## [1] 7
vec1 <- 1:n_dims^2
print(vec1)
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
vec1_shuffled <- sample(vec1)
print(vec1_shuffled)
## [1] 42 23 7 11 37 49 29 48 47 31 10 1 32 36 25 24 40 16 22 34 6 19 15 20 44
## [26] 41 38 26 28 12 4 21 2 39 35 33 13 14 9 17 43 46 18 30 27 5 3 8 45
The variable n_dims was set to be a single random integer between 3
and 10, and before every re-run, the number previously sampled is
replaced. A vector was then made holding all the integers between one
and the value of n_dims squared. The vector was then shuffled with
sample()
to randomly organize the integers.
m1 <- matrix(data=vec1_shuffled, nrow=n_dims, ncol=n_dims)
print(m1)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 42 48 25 19 28 33 18
## [2,] 23 47 24 15 12 13 30
## [3,] 7 31 40 20 4 14 27
## [4,] 11 10 16 44 21 9 5
## [5,] 37 1 22 41 2 17 3
## [6,] 49 32 34 38 39 43 8
## [7,] 29 36 6 26 35 46 45
tm1 <- t(m1)
print(tm1)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 42 23 7 11 37 49 29
## [2,] 48 47 31 10 1 32 36
## [3,] 25 24 40 16 22 34 6
## [4,] 19 15 20 44 41 38 26
## [5,] 28 12 4 21 2 39 35
## [6,] 33 13 14 9 17 43 46
## [7,] 18 30 27 5 3 8 45
A matrix was then made using the values from the shuffled vector. The
number of rows and columns were both set to be the value of n_dims so
that the matrix will remain a square no matter what value n_dims is.
Once the matrix was made, it was transposed using t()
.
sum_tm1_r1 <- sum(tm1[1,])
mean_tm1_r1 <- mean(tm1[1,])
sum_tm1_r6 <- sum(tm1[6,])
mean_tm1_r6 <- mean(tm1[6,])
The sums and means of rows 1 and 6 of the transposed matrix were
found using the sum()
and mean()
function. To
isolate the rows, a braket with the row value then a comma was put after
the matrix name.
eigen_tm1 <- eigen(tm1)
typeof(eigen_tm1$values) #complex
## [1] "complex"
typeof(eigen_tm1$vectors) #complex
## [1] "complex"
The eigen values and vectors for the transposed matrix were
calculated using the eigen()
function. To determine the
type of numbers the values and vectors were, the typeof()
function was used and a $
was used to look at the values
and vectors individually.
my_matrix <- matrix(sample(1:100, 16), nrow=4, ncol=4)
print(my_matrix)
## [,1] [,2] [,3] [,4]
## [1,] 96 47 83 19
## [2,] 41 98 39 50
## [3,] 36 28 18 92
## [4,] 22 58 73 75
set_logical <- sample(1:20,100, replace = TRUE)
my_logical <- set_logical > 10
print(my_logical)
## [1] TRUE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE TRUE TRUE TRUE
## [13] FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE TRUE TRUE TRUE
## [25] FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE FALSE
## [37] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE TRUE
## [49] TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE
## [61] FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE
## [73] TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE
## [85] TRUE TRUE FALSE FALSE TRUE FALSE TRUE TRUE TRUE TRUE FALSE FALSE
## [97] TRUE FALSE FALSE TRUE
my_letters <- sample(letters[seq(from=1, to=26)])
print(my_letters)
## [1] "v" "g" "d" "u" "e" "z" "o" "k" "m" "n" "p" "b" "y" "w" "f" "r" "i" "j" "q"
## [20] "t" "x" "c" "h" "a" "s" "l"
The variable my_matrix
was assigned a matrix of 4 rows
and 4 columns with filled with random integers between 1 and 100 using
the sample()
function and matrix specifications. The
variable my_logical
was assigned a 100-element vector TRUE
and FALSE values. This was achieved by first making a vector
(set_logical
) with 100 numbers between 1 and 20;
my_logical
was then set evaluate whether the numbers in
set_logical
were greater than 10. This returned a 100 long
vector of TRUE and FALSE. The variable my_letters
sequenced
the alphabet using letters[]
(1 to 26 because there are 26
letters) and used sample()
to scramble that sequence.
typeof(my_matrix[2,2]) #double
## [1] "integer"
typeof(my_logical[2]) #logical
## [1] "logical"
typeof(my_letters[2]) #character
## [1] "character"
vec2 <- c(my_matrix[2,2], my_logical[2], my_letters[2])
typeof(vec2) #character
## [1] "character"
The types of elements were determined for an element from each of the
vectors. A new vector was then made with those three elements using
c()
, and the type of objects in the vector was
determined.
df <- data.frame(my_unis=sample(1:10, 26, replace=TRUE), my_letters=sample(LETTERS[seq(from=1, to=26)]))
print(df)
## my_unis my_letters
## 1 4 N
## 2 5 U
## 3 5 B
## 4 3 L
## 5 1 Z
## 6 7 M
## 7 1 E
## 8 10 D
## 9 1 F
## 10 8 V
## 11 4 A
## 12 10 C
## 13 7 X
## 14 6 H
## 15 3 J
## 16 3 Q
## 17 9 O
## 18 10 G
## 19 7 P
## 20 3 I
## 21 9 T
## 22 1 W
## 23 6 Y
## 24 10 K
## 25 3 R
## 26 6 S
The variable df was assigned a data frame with columns my_unis and my_letters. The rows of each column were filled by assigning my_unis and my_letters to vectors of elements.
df$my_unis <- df$my_unis[replace(df$my_unis, sample(1:26, 4), NA)]
print(df)
## my_unis my_letters
## 1 3 N
## 2 1 U
## 3 1 B
## 4 5 L
## 5 NA Z
## 6 1 M
## 7 4 E
## 8 8 D
## 9 4 F
## 10 10 V
## 11 3 A
## 12 8 C
## 13 1 X
## 14 7 H
## 15 5 J
## 16 5 Q
## 17 1 O
## 18 8 G
## 19 NA P
## 20 5 I
## 21 NA T
## 22 NA W
## 23 7 Y
## 24 8 K
## 25 5 R
## 26 7 S
which(is.na(df$my_unis))
## [1] 5 19 21 22
The column my_unis of the data frame, df, had 4 random rows
(randomized by sample()
) replaced with NA. The locations of
the NA values were then found using the which(is.na())
function.
df <- df[order(df$my_letters),]
print(df)
## my_unis my_letters
## 11 3 A
## 3 1 B
## 12 8 C
## 8 8 D
## 7 4 E
## 9 4 F
## 18 8 G
## 14 7 H
## 20 5 I
## 15 5 J
## 24 8 K
## 4 5 L
## 6 1 M
## 1 3 N
## 17 1 O
## 19 NA P
## 16 5 Q
## 25 5 R
## 26 7 S
## 21 NA T
## 2 1 U
## 10 10 V
## 22 NA W
## 13 1 X
## 23 7 Y
## 5 NA Z
The column my_letters of the data fram was ordered using the
order()
function.