NAME: MUHAMAD RAFIQ IQBAL BIN SAMSUDIN MATRIC : 17206926
#Create a Vector named num
num <- c(2,0,4,6)
#Print The 3rd Element
print(num[3])
## [1] 4
print(num[2:4])
## [1] 0 4 6
#Use function length() to count the element in the vector
print(length(num))
## [1] 4
#Create a vector name q3
q3 <- c(3, 5)
# Add 100 to the vector
q3 <- q3 + 100
# Display the output
print(q3)
## [1] 103 105
#Create a vector named animal
animal <- c("cat","tiger","lion","elephant")
# Display the output
print("The Output of Animal Vector:")
## [1] "The Output of Animal Vector:"
print(animal)
## [1] "cat" "tiger" "lion" "elephant"
#Append new animal
animal <- c(animal,"monkey","cow")
#Display new output
print("After adjustment:")
## [1] "After adjustment:"
print(animal)
## [1] "cat" "tiger" "lion" "elephant" "monkey" "cow"
# Create two integer vectors
n1 <- c(2, 4, 6) # Example integers
n2 <- c(1, 3, 5) # Example integers
# Add the two vectors
addition_result <- n1 + n2
# Multiply the two vectors
multiplication_result <- n1 * n2
# Display the results
print("Vector n1:")
## [1] "Vector n1:"
print(n1)
## [1] 2 4 6
print("Vector n2:")
## [1] "Vector n2:"
print(n2)
## [1] 1 3 5
print("Addition result:")
## [1] "Addition result:"
print(addition_result)
## [1] 3 7 11
print("Multiplication result:")
## [1] "Multiplication result:"
print(multiplication_result)
## [1] 2 12 30
# Create a vector x of size 4 with values from 1 to 10
x <- c(3.53, 7, 1, 9) # Example values
# Calculate sum, mean, minimum, and maximum by using the predifined function
sum_x <- sum(x)
mean_x <- round(mean(x),2)#To round up to 2 decimal places
min_x <- min(x)
max_x <- max(x)
# Display the results
print("Vector x:")
## [1] "Vector x:"
print(x)
## [1] 3.53 7.00 1.00 9.00
print("Sum of x:")
## [1] "Sum of x:"
print(sum_x)
## [1] 20.53
print("Mean of x:")
## [1] "Mean of x:"
print(mean_x)
## [1] 5.13
print("Minimum of x:")
## [1] "Minimum of x:"
print(min_x)
## [1] 1
print("Maximum of x:")
## [1] "Maximum of x:"
print(max_x)
## [1] 9
# Repeat the same step but firstly we append 3 new value to vector X
x <- c(x, 11, 15, 20)
# Calculate sum, mean, minimum, and maximum
sum_x <- sum(x)
mean_x <- round(mean(x),2) #To round up to 2 decimal places
min_x <- min(x)
max_x <- max(x)
# Display the results
print("Updated vector x:")
## [1] "Updated vector x:"
print(x)
## [1] 3.53 7.00 1.00 9.00 11.00 15.00 20.00
print("Sum of x:")
## [1] "Sum of x:"
print(sum_x)
## [1] 66.53
print("Mean of x:")
## [1] "Mean of x:"
print(mean_x)
## [1] 9.5
print("Minimum of x:")
## [1] "Minimum of x:"
print(min_x)
## [1] 1
print("Maximum of x:")
## [1] "Maximum of x:"
print(max_x)
## [1] 20
#Print the first two values
print(x[1:2])
## [1] 3.53 7.00
#Print the last two values
print(x[6:7])
## [1] 15 20
# To sort vector we use predefined function sort() and rev(x)
s1<- sort(x)
s2<- sort(x,decreasing =TRUE)
s3<- rev(x)
#Print all output
print("Vector in ascending order (s1):")
## [1] "Vector in ascending order (s1):"
print(s1)
## [1] 1.00 3.53 7.00 9.00 11.00 15.00 20.00
print("Vector in descending order (s2):")
## [1] "Vector in descending order (s2):"
print(s2)
## [1] 20.00 15.00 11.00 9.00 7.00 3.53 1.00
print("Vector in reverse order (s3):")
## [1] "Vector in reverse order (s3):"
print(s3)
## [1] 20.00 15.00 11.00 9.00 1.00 7.00 3.53
second_highest <- sort(unique(x), decreasing = TRUE)[2]
# Sort unique values and select the second. This sorts the unique values of x in descending order, ensuring duplicates are removed.
print("Second highest value in vector x:")
## [1] "Second highest value in vector x:"
print(second_highest)
## [1] 15