Please input your student name and No. below.
Student Name: li xiaofei
Student No.: 2230026083


Q1. Let \(x\) be defined as follow.
a. Add 3 to just the even-index elements.
b. Compute the square root of each element.

x <- c(11,10,5,9,14,12,18,2,17,9,1,7,1,15,16,7,16,12,13,3,8,3,11,1,5,7,7,10,6,10,17,13,1,2,17,1,15,8,12,3,6,15,10,3,10,12,19,17,15,12,18,17,1,13,5,9,18,2,20,16,15,7,9,12,12,5,5,7,7,9,17,7,15,11,10,12,19,8,13,3,7,14,13,18,1,11,18,1,12,18,5,12,5,10,3,6,11,12,1,3)
# Write your code here
# a. Add 3 to just the even-index elements
x[seq(2, length(x), by=2)] <- x[seq(2, length(x), by=2)] + 3

# b. Compute the square root of each element
x <- sqrt(x)

Q2. Given x = [3 15 9 12 -1 -12 9 6 1], provide the command that will
a) set the values of x that are positive to zero
b) set values that are multiples of 3 to 3
c) multiply the values of x that are even by 5
d) extract the values of x that are greater than 10 into a vector called y
e) Find the index position of elements in x which are larger than 4

# Write your code here
# Given x
x <- c(3, 15, 9, 12, -1, -12, 9, 6, 1)

# a) Set the values of x that are positive to zero
x[x > 0] <- 0

# b) Set values that are multiples of 3 to 3
x[x %% 3 == 0] <- 3

# c) Multiply the values of x that are even by 5
x[x %% 2 == 0] <- x[x %% 2 == 0] * 5

# d) Extract the values of x that are greater than 10 into a vector called y
y <- x[x > 10]

# e) Find the index position of elements in x which are larger than 4
index_positions <- which(x > 4)

Q3. Given the following list, please answer the questions below.

# Create a list containing a vector, a matrix and a list.
list_data <- list(c("Jan", "Feb", "Mar"), matrix(c(3, 9, 5, 1, -2, 8), nrow = 2), list("green", 12.3))
# Give names to the elements in the list.
names(list_data) <- c("1st Quarter", "A_Matrix", "An Inner list")
  1. Access the first element of the list using position.

  2. Access the last element of the list using name.

  3. Add element “New element” at the end of the list.

  4. Remove the last element.

# Write your code here
# Create a list containing a vector, a matrix, and a list.
list_data <- list(c("Jan", "Feb", "Mar"), matrix(c(3, 9, 5, 1, -2, 8), nrow = 2), list("green", 12.3))

# Give names to the elements in the list
names(list_data) <- c("1st Quarter", "A_Matrix", "An Inner list")

# Access the first element of the list using position
list_data[[1]]
## [1] "Jan" "Feb" "Mar"
# Access the last element of the list using name
list_data[["An Inner list"]]
## [[1]]
## [1] "green"
## 
## [[2]]
## [1] 12.3
# Add element “New element” at the end of the list
list_data[["New element"]] <- "New element"

# Remove the last element
list_data <- list_data[-length(list_data)]

Q4. Given the matrix A as following

a <- c(2,4,1,6,7,2,3,5,9)
A <- matrix(a, nrow=3, ncol=3, byrow=T)
A
##      [,1] [,2] [,3]
## [1,]    2    4    1
## [2,]    6    7    2
## [3,]    3    5    9

provide the commands needed to
a) assign the last 2 rows of A to a matrix called y
b) compute the sum over the columns of A
c) compute the sum over the rows of A
Hint: you can use function apply

# Write your code here
# Given matrix A
a <- c(2, 4, 1, 6, 7, 2, 3, 5, 9)
A <- matrix(a, nrow = 3, ncol = 3, byrow = TRUE)

# a) Assign the last 2 rows of A to a matrix called y
y <- A[2:3, ]

# b) Compute the sum over the columns of A
col_sum <- apply(A, 2, sum)

# c) Compute the sum over the rows of A
row_sum <- apply(A, 1, sum)