Q1

In each case, what is the value of x?

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

Q2

Shorten the notation of following vectors

print(x<-c(157:164))
## [1] 157 158 159 160 161 162 163 164
print(x<-c(10:1))
##  [1] 10  9  8  7  6  5  4  3  2  1
x1<-seq(-1071, by=-1, length.out=5)
x2<-seq(-1074, by=1, length.out=4)
print(append(x1, x2, after=5) )
## [1] -1071 -1072 -1073 -1074 -1075 -1074 -1073 -1072 -1071
print(x<-seq(1.5, by=1, length.out=5))
## [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:

a.A sequence of 10 numbers from 20-11

print(y<-seq(20, 11, -1))
##  [1] 20 19 18 17 16 15 14 13 12 11

b.A sequence of odd numbers from 11-20

print(y<-seq(11, 20, 2))
## [1] 11 13 15 17 19

c.A sequence of first twelve square number starting from 1.

y<-seq(1, 12, 1)
print(y<-y*y)
##  [1]   1   4   9  16  25  36  49  64  81 100 121 144

d.A sequence of first eleven exponential number of 2 starting from 1.

y<-seq(1, 11, 1)
print(y<-2^y)
##  [1]    2    4    8   16   32   64  128  256  512 1024 2048

Q5

Create a vector z based on the requirements below:

a.A sequence of 10 W

print(z<- rep("w", times=10))
##  [1] "w" "w" "w" "w" "w" "w" "w" "w" "w" "w"

b.A sequence of R R R S S S

print(z<- rep(c("R", "S"), each=3))
## [1] "R" "R" "R" "S" "S" "S"

c.The first 5 alphabets in lower case

print(z<- letters[1:5])
## [1] "a" "b" "c" "d" "e"

d.A sequence of players from Player1 – Player10

z<- rep("Player", times=10)
print(z<- paste0(z, 1:10))
##  [1] "Player1"  "Player2"  "Player3"  "Player4"  "Player5"  "Player6" 
##  [7] "Player7"  "Player8"  "Player9"  "Player10"

Q6

Create vectors as below.

a.Display the vector

Mtut1<- c(15, 17, 10, 8, 19)
names(Mtut1)[1:5]<-c("Ali", "Abu", "Ahmad", "Bala", "Chong")
print(Mtut1)
##   Ali   Abu Ahmad  Bala Chong 
##    15    17    10     8    19
Mtut2<- c(5, 4, 3, 5, 4)
names(Mtut2)[1:5]<-c("Ali", "Abu", "Ahmad", "Bala", "Chong")
print(Mtut2)
##   Ali   Abu Ahmad  Bala Chong 
##     5     4     3     5     4

b.What is the total mark for Abu?

print(x<-Mtut1[2]+Mtut2[2])
## Abu 
##  21

c.Display the percentage for each student in two decimal places if the total mark is 30.

sprintf("%0.2f%%", Mute1<-(Mtut1/30)*100)
## [1] "50.00%" "56.67%" "33.33%" "26.67%" "63.33%"

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.

print(num<- sample(51:100, 10, replace=FALSE))
##  [1] 55 98 72 92 59 86 80 91 75 88
print(even<-num[num%%2==0])
## [1] 98 72 92 86 80 88

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:

#cat(“Enter the length in inches :”)

#a<-as.numeric(readLines(“stdin”, 1))

#cat(“21.80 inches =”, a*2.54, “centimeters”)