============================================================

THE CO-OPERATIVE UNIVERSITY OF KENYA

BAST 3152: STATISTICAL PROGRAMMING

PRACTICAL 1 - R PROGRAMMING SOLUTIONS

install.packages(“tinytex”) tinytex::install_tinytex() tinytex::install_tinytex() # ———————————————————— # QUESTION 1: THE rep() FUNCTION # ————————————————————

cat(“\n================ QUESTION 1 ================\n”)

1(a) Repeat 2 four times, 3 three times, and 5 two times.

answer_1a <- rep(c(2, 3, 5), 4:2) cat(“\n1(a) rep(c(2, 3, 5), 4:2):\n”) print(answer_1a)

1(b) Obtain four 4s, four 3s, and four 2s.

answer_1b <- rep(c(4, 3, 2), each = 4) cat(“\n1(b) Four 4s, four 3s, and four 2s:\n”) print(answer_1b)

1(c) Repeat the sequence 3, 1, 1, 5, 7 until the vector

contains exactly 50 elements.

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”)

1(d) Repeat each element of the sequence four times.

answer_1d_each <- rep(c(3, 1, 1, 5, 7), each = 4) cat(“\n1(d) Using the each argument:\n”) print(answer_1d_each)

The same result can be obtained without using each.

Each original element is repeated separately and the results

are combined using c().

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)

————————————————————

QUESTION 2: VECTORS AND LISTS

————————————————————

cat(“\n================ QUESTION 2 ================\n”)

2(a) Create four vectors.

The practical does not specify the actual observations.

The following are illustrative values.

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)

2(b) Create a list and give every component a name.

mylist <- list( Year = Year, mean_weight = mean_weight, Gender = Gender, mean_height = mean_height )

cat(“\n2(b) The named list, mylist:\n”) print(mylist)

2(c) Access the fourth element in two different ways.

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])

————————————————————

QUESTION 3: DATA FRAME AND MATRIX

————————————————————

cat(“\n================ QUESTION 3 ================\n”)

Record the five students’ weights before and after the programme.

before <- c(78, 72, 78, 79, 105) after <- c(67, 65, 79, 70, 93)

Convert the two vectors into a data frame.

weight_data <- data.frame( Student = 1:5, Before = before, After = after )

cat(“\n3. Data frame of students’ weights:\n”) print(weight_data)

Convert the vectors into a matrix with two columns.

weight_matrix <- cbind( Before = before, After = after )

cat(“\n3. Matrix of students’ weights:\n”) print(weight_matrix)

————————————————————

QUESTION 4: MATRIX CREATION AND MATRIX OPERATIONS

————————————————————

cat(“\n================ QUESTION 4 ================\n”)

Create matrix A with 10 rows and 3 columns.

The first and last rows follow the practical.

Any numbers may be used in the dotted middle rows.

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) )

Create matrix B with 10 rows and 3 columns.

The specified first, second, third, and last rows are retained.

Any numbers may be used in the dotted middle rows.

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)

Confirm the dimensions of both matrices.

cat(“\nDimensions of A:\n”) print(dim(A))

cat(“\nDimensions of B:\n”) print(dim(B))

4(a) Multiply every element of A by 2.

result_4a <- 2 * A cat(“\n4(a) 2A:\n”) print(result_4a)

4(b) Multiply every element of B by 5.

result_4b <- 5 * B cat(“\n4(b) 5B:\n”) print(result_4b)

4(c) The printed coefficient in the supplied PDF is unclear.

This line interprets the expression as 3A - 3B.

If your lecturer intended different coefficients, change them here.

result_4c <- 3 * A - 3 * B cat(“\n4(c) Interpreted as 3A - 3B:\n”) print(result_4c)

4(d) Add matrices A and B element by element.

result_4d <- A + B cat(“\n4(d) A + B:\n”) print(result_4d)