install.packages(“tinytex”) tinytex::install_tinytex() tinytex::install_tinytex() # ———————————————————— # QUESTION 1: THE rep() FUNCTION # ————————————————————
cat(“\n================ QUESTION 1 ================\n”)
answer_1a <- rep(c(2, 3, 5), 4:2) cat(“\n1(a) rep(c(2, 3, 5), 4:2):\n”) print(answer_1a)
answer_1b <- rep(c(4, 3, 2), each = 4) cat(“\n1(b) Four 4s, four 3s, and four 2s:\n”) print(answer_1b)
answer_1c <- rep(c(3, 1, 1, 5, 7), length.out = 50) cat(“\n1(c) Repeated sequence with length 50:\n”) print(answer_1c) cat(“Length of answer_1c:”, length(answer_1c), “\n”)
answer_1d_each <- rep(c(3, 1, 1, 5, 7), each = 4) cat(“\n1(d) Using the each argument:\n”) print(answer_1d_each)
sequence <- c(3, 1, 1, 5, 7) answer_1d_without_each <- c( rep(sequence[1], 4), rep(sequence[2], 4), rep(sequence[3], 4), rep(sequence[4], 4), rep(sequence[5], 4) ) cat(“\n1(d) Without using the each argument:\n”) print(answer_1d_without_each)
cat(“\n================ QUESTION 2 ================\n”)
Year <- c(2022, 2023, 2024, 2025, 2026) mean_weight <- c(58.5, 60.2, 61.8, 62.4, 64.1) Gender <- c(“Female”, “Male”, “Female”, “Male”, “Female”) mean_height <- c(160.5, 172.3, 162.1, 175.0, 164.8)
cat(“\n2(a) The four vectors:\n”) cat(“Year:\n”) print(Year) cat(“mean_weight:\n”) print(mean_weight) cat(“Gender:\n”) print(Gender) cat(“mean_height:\n”) print(mean_height)
mylist <- list( Year = Year, mean_weight = mean_weight, Gender = Gender, mean_height = mean_height )
cat(“\n2(b) The named list, mylist:\n”) print(mylist)
fourth_element_method_1 <- mylist[[4]] fourth_element_method_2 <- mylist$mean_height
cat(“\n2(c) Fourth element using double square brackets:\n”) print(fourth_element_method_1)
cat(“\n2(c) Fourth element using the component name:\n”) print(fourth_element_method_2)
cat(“\nName of the fourth list component:\n”) print(names(mylist)[4])
cat(“\n================ QUESTION 3 ================\n”)
before <- c(78, 72, 78, 79, 105) after <- c(67, 65, 79, 70, 93)
weight_data <- data.frame( Student = 1:5, Before = before, After = after )
cat(“\n3. Data frame of students’ weights:\n”) print(weight_data)
weight_matrix <- cbind( Before = before, After = after )
cat(“\n3. Matrix of students’ weights:\n”) print(weight_matrix)
cat(“\n================ QUESTION 4 ================\n”)
A <- rbind( c(1, 3, 5), c(1, 2, 4), c(1, 4, 6), c(1, 5, 2), c(1, 6, 7), c(1, 2, 3), c(1, 7, 4), c(1, 5, 6), c(1, 4, 2), c(1, 3, 3) )
B <- rbind( c(1, 3, 5), c(1, 7, 4), c(1, 5, 0), c(1, 2, 6), c(1, 4, 3), c(1, 6, 2), c(1, 2, 7), c(1, 5, 4), c(1, 4, 6), c(1, 3, -1) )
cat(“\nMatrix A:\n”) print(A)
cat(“\nMatrix B:\n”) print(B)
cat(“\nDimensions of A:\n”) print(dim(A))
cat(“\nDimensions of B:\n”) print(dim(B))
result_4a <- 2 * A cat(“\n4(a) 2A:\n”) print(result_4a)
result_4b <- 5 * B cat(“\n4(b) 5B:\n”) print(result_4b)
result_4c <- 3 * A - 3 * B cat(“\n4(c) Interpreted as 3A - 3B:\n”) print(result_4c)
result_4d <- A + B cat(“\n4(d) A + B:\n”) print(result_4d)