Student Information

NAME: MUHAMAD RAFIQ IQBAL BIN SAMSUDIN MATRIC : 17206926

2. Create a vector named num with four elements (2,0,4,6 )

a. Print the third Element

#Create a Vector named num

num <- c(2,0,4,6)

#Print The 3rd Element

print(num[3])
## [1] 4

b. Display all the elements of the vector except first element

print(num[2:4])
## [1] 0 4 6

c. Count the number of elements in the vector.

#Use function length() to count the element in the vector

print(length(num))
## [1] 4

3. Create a vector named q3 that add two numbers 3 and 5. After that, add 100 to this vector and display the output

#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

4. Create a vector named animal that consists of cat, tiger, lion and elephant. Display the vector. After that, append monkey and cow to the vector and display the output

#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"

5. Create two vectors named n1 and n2 of integers type (any number) and length 3 Then, add and multiply the two vectors.

# 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

6. Create a vector x of size 4 with any value from 1-10.

a. Display the sum, mean, minimum and the maximum of the vector x

# 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

b.Append 3 values (11-20) to the vector x created. Display the sum, mean, minimum and the maximum of the vector.

# 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

c.Display the first two values and last two values of vector x

#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

d.Assign the vector x in ascending order to s1, descending order to s2 and reverse order to s3.

# 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

e. Display the second highest value in vector X.

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