n_dims <- sample(3:10,1)
print(n_dims)
## [1] 3
my_vec <- seq(from=1, to=n_dims^2)
print(my_vec)
## [1] 1 2 3 4 5 6 7 8 9
# Since it was random when n_dims result was 3 the my_vec result was:
[1] 1 2 3 4 5 6 7 8 9
#When n_dims result was 9 the my_vec result was:
[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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
[44] 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
shuffled_vector <- sample(my_vec)
print(shuffled_vector)
## [1] 3 5 2 1 6 9 4 8 7
m <- matrix(data=my_vec,nrow=n_dims)
print(m)
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
Find a function in r to transpose the matrix.print it out again and note how it has changed.
my_matrix=matrix(data=my_vec,nrow=n_dims)
transposed_matrix <- t(my_matrix)
print(transposed_matrix)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
first_row_sum <- sum(transposed_matrix[1,])
print(first_row_sum)
## [1] 6
first_row_mean <- mean(transposed_matrix[1,])
print(first_row_mean)
## [1] 2
last_row_sum <- sum(transposed_matrix[n_dims,])
print (last_row_sum)
## [1] 24
last_row_mean <- mean(transposed_matrix[n_dims,])
print(last_row_mean)
## [1] 8
ev <- eigen(transposed_matrix)
(values <- ev$values)
## [1] 1.611684e+01 -1.116844e+00 -1.303678e-15
(vectors <- ev$vectors)
## [,1] [,2] [,3]
## [1,] -0.2319707 -0.78583024 0.4082483
## [2,] -0.5253221 -0.08675134 -0.8164966
## [3,] -0.8186735 0.61232756 0.4082483
typeof(vectors)
## [1] "double"
typeof(values)
## [1] "double"
#Create a list with the following named elements: #my_matrix, which is a 4 x 4 matrix filled with random uniform values
my_matrix=matrix(runif(16),nrow=4,ncol=4)
print(my_matrix)
## [,1] [,2] [,3] [,4]
## [1,] 0.736595992 0.5407618 0.7653967 0.8104423
## [2,] 0.216964912 0.6941158 0.4224923 0.9351292
## [3,] 0.416509726 0.6056447 0.3174344 0.9269677
## [4,] 0.006147732 0.6453225 0.2340890 0.2914202
#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 <- sample(c(TRUE, FALSE), size = 100, replace = TRUE, prob = c(0.5, 0.5))
print(my_logical)
## [1] FALSE TRUE FALSE FALSE TRUE FALSE FALSE TRUE TRUE TRUE FALSE TRUE
## [13] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE
## [25] TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE
## [37] TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE TRUE TRUE
## [49] FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE
## [61] FALSE TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
## [73] FALSE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE FALSE FALSE
## [85] FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE FALSE TRUE FALSE
## [97] FALSE FALSE FALSE FALSE
#my_letters, which is a 26-element vector of all the lower-case letters in random order.
my_letters <- sample(letters, 26)
print(my_letters)
## [1] "z" "k" "n" "v" "x" "d" "b" "a" "h" "s" "c" "t" "y" "l" "j" "u" "q" "m" "e"
## [20] "p" "f" "o" "g" "r" "w" "i"
#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 <- list(my_matrix,
my_logical,
my_letters)
my_list <- list(my_matrix[[2,2]], my_logical[2], my_letters[2])
print(my_list)
## [[1]]
## [1] 0.6941158
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] "k"
#use the typeof() function to confirm the underlying data types of each component in this list
type_my_matrix <- typeof(my_matrix[[2,2]])
print(type_my_matrix)
## [1] "double"
type_my_logical <- typeof(my_logical[2])
print(type_my_logical)
## [1] "logical"
type_my_letters <- typeof(my_letters[2])
print(type_my_letters)
## [1] "character"
#combine the underlying elements from the new list into a single atomic vector with the c() function.
vector2 <- c(my_matrix[[2,2]],my_logical[2],my_letters[2])
print(vector2)
## [1] "0.694115840131417" "TRUE" "k"
#what is the data type of this vector?
typeof(vector2)
## [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_units <- runif(n=26, min=0, max=10)
my_letters <- sample(LETTERS, size = 26)
d_frame <- data.frame(my_units,my_letters)
print(d_frame)
## my_units my_letters
## 1 9.23722138 T
## 2 8.47982747 Z
## 3 3.44000402 P
## 4 7.17119488 F
## 5 2.97713941 E
## 6 4.66658244 G
## 7 1.14075674 R
## 8 3.37299993 D
## 9 1.28811021 X
## 10 0.08445377 K
## 11 8.64458273 W
## 12 7.24231186 S
## 13 1.89699734 N
## 14 9.10409742 B
## 15 1.86964123 J
## 16 9.43366003 C
## 17 8.05421995 M
## 18 1.26174995 H
## 19 7.11246095 Y
## 20 3.11415730 V
## 21 5.59232361 O
## 22 7.95570455 L
## 23 3.84411380 A
## 24 0.13315574 I
## 25 9.07888778 U
## 26 8.19313011 Q
str(d_frame)
## 'data.frame': 26 obs. of 2 variables:
## $ my_units : num 9.24 8.48 3.44 7.17 2.98 ...
## $ my_letters: chr "T" "Z" "P" "F" ...
#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.
d_frame[sample(x=1:26,4),1] <- NA
print(d_frame)
## my_units my_letters
## 1 9.23722138 T
## 2 8.47982747 Z
## 3 NA P
## 4 7.17119488 F
## 5 2.97713941 E
## 6 NA G
## 7 NA R
## 8 3.37299993 D
## 9 1.28811021 X
## 10 0.08445377 K
## 11 8.64458273 W
## 12 7.24231186 S
## 13 1.89699734 N
## 14 9.10409742 B
## 15 1.86964123 J
## 16 9.43366003 C
## 17 8.05421995 M
## 18 1.26174995 H
## 19 7.11246095 Y
## 20 NA V
## 21 5.59232361 O
## 22 7.95570455 L
## 23 3.84411380 A
## 24 0.13315574 I
## 25 9.07888778 U
## 26 8.19313011 Q
#For the first variable, write a single line of R code to identify which rows have the missing values.
which(is.na(d_frame), arr.ind=TRUE)
## row col
## [1,] 3 1
## [2,] 6 1
## [3,] 7 1
## [4,] 20 1
#Re-order the entire data frame to arrange the second variable in alphabetical order
my_units <- runif(n=26, min=0, max=10)
my_letters <- rep(c(LETTERS))
d_frame <- data.frame(my_units,my_letters)
print(d_frame)
## my_units my_letters
## 1 6.6528318 A
## 2 1.7905158 B
## 3 5.3809723 C
## 4 9.5863125 D
## 5 1.8385999 E
## 6 4.4932203 F
## 7 8.0846650 G
## 8 7.0989842 H
## 9 8.9878125 I
## 10 9.2937142 J
## 11 7.1883220 K
## 12 8.0580807 L
## 13 4.5670943 M
## 14 0.7208192 N
## 15 3.6523246 O
## 16 3.8259048 P
## 17 6.0233280 Q
## 18 0.4269421 R
## 19 6.9307629 S
## 20 8.4384231 T
## 21 1.6555957 U
## 22 6.5315454 V
## 23 7.9337994 W
## 24 9.2966330 X
## 25 0.6757230 Y
## 26 9.0141674 Z
#Calculate the column mean for the first variable.
mean(d_frame[, 1])
## [1] 5.697965