This is the Lab 2 practice for subject WQD7004 Programming for Data Science in University of Malaya, Semester 1 2025/2026.

Q1

In each case, what is the value of x?

x<-2-1*2
x
## [1] 0
x<-6/3-2+1*0+3/3-3
x
## [1] -2
x<-19%%17%%13
x
## [1] 2
x<-(19%%17)%%13
x
## [1] 2
x<-19%%(17%%13)
x
## [1] 3
x<-2^17%%17
x
## [1] 2
x<-3-2%%5+3*2-4/2
x
## [1] 5

Q2

Shorten the notation of following vectors

x <- 157:164
x
## [1] 157 158 159 160 161 162 163 164
x <- 10:1
x
##  [1] 10  9  8  7  6  5  4  3  2  1
x <- c(-1071:-1074,-1075:-1071)
x
## [1] -1071 -1072 -1073 -1074 -1075 -1074 -1073 -1072 -1071
x <- 1.5:5.5
x
## [1] 1.5 2.5 3.5 4.5 5.5

Q3

Create a vector x of with the following value (0.15, 1.30, 3.45, 5.75). Then display the vector in character and integer.

x <- c(0.15,1.30,3.45,5.75)
print(as.character(x))
## [1] "0.15" "1.3"  "3.45" "5.75"
print(as.integer(x))
## [1] 0 1 3 5

Q4

Create a vector y based on the requirements below:

  1. A sequence of 10 numbers from 20-11
y <- 20:11
y
##  [1] 20 19 18 17 16 15 14 13 12 11
  1. A sequence of odd numbers from 11-20
y <- seq(11, 20, 2)
y
## [1] 11 13 15 17 19
  1. A sequence of first twelve square number starting from 1.
y <- (1:12)^2
y
##  [1]   1   4   9  16  25  36  49  64  81 100 121 144
  1. A sequence of first eleven exponential number of 2 starting from 1.
y <- 2^(0:10)
y
##  [1]    1    2    4    8   16   32   64  128  256  512 1024

Q5

Create a vector z based on the requirements below:
a. A sequence of 10 W

z_a <- rep("W",times=10)
z_a
##  [1] "W" "W" "W" "W" "W" "W" "W" "W" "W" "W"
  1. A sequence of R R R S S S
z_b <- rep(c("R","S"), each=3)
z_b
## [1] "R" "R" "R" "S" "S" "S"
  1. The first 5 alphabets in lower case
z_c <- letters[1:5]
z_c
## [1] "a" "b" "c" "d" "e"
  1. A sequence of players from Player1 – Player10
z_d <- paste0("Player",1:10)
z_d
##  [1] "Player1"  "Player2"  "Player3"  "Player4"  "Player5"  "Player6" 
##  [7] "Player7"  "Player8"  "Player9"  "Player10"

Q6

Create vectors as below.
Lab 2 Q6 vector
a. Display the vector

Mtut1 <- c("Ali" = 15, "Abu" = 17, "Ahmad" = 10, "Bala" = 8, "Chong" = 19)
Mtut1
##   Ali   Abu Ahmad  Bala Chong 
##    15    17    10     8    19
students <- c("Ali","Abu","Ahmad","Bala","Chong")
Mtut2 <- c(5,4,3,5,4)
names(Mtut2) <- students
Mtut2
##   Ali   Abu Ahmad  Bala Chong 
##     5     4     3     5     4
  1. What is the total mark for Abu?
total_Abu <- Mtut1["Abu"] + Mtut2["Abu"]
total_Abu
## Abu 
##  21
  1. Display the percentage for each student in two decimal places if the total mark is 30.
percentage <- round(((Mtut1 + Mtut2)/30)*100,2)
percentage
##   Ali   Abu Ahmad  Bala Chong 
## 66.67 70.00 43.33 43.33 76.67

Q7

Create a vector num of size 10 with any random value from 51-100. Display the vector and then assign all the even numbers to a new vector named even.

num <- sample(51:100, size=10, replace=F)
num
##  [1] 86 76 84 53 85 99 63 97 59 51
even <- num[num%%2 == 0]
even
## [1] 86 76 84

Q8

Create an R file named convert.r that used to convert inch to centimeters. Given 1 inch equals to 2.54 centimeters. Display the value of centimeters in two decimal places. Run the r file using terminal. Example output:
Lab 2 Q8 output

convert.r file:

cat("Enter the length in inches : ")
input <- readLines("stdin", 1)
l_inch <- as.numeric(input)
l_cm <- round(l_inch*2.54, 2)
cat(formatC(l_inch, format = "f", digits=2), " inches = ", l_cm, " centimetres.")

Q9

Create an R file named sales.r that get the price of item from the user and then display the new discount price for the item based on discounts of 50%, 30% and 10%. Run the r file using terminal. Example output:
Lab 2 Q8 output

sales.r file:

cat("Enter the price of items : ")
input <- readLines("stdin", 1)
price <- as.numeric(input)

cat("The price of item after 50% discount is ", round(price*50/100, 2), "\n")
cat("The price of item after 30% discount is ", round(price*30/100, 2), "\n")
cat("The price of item after 10% discount is ", round(price*10/100, 2), "\n")