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
- Shorten the notation of following vectors x<-c(157, 158, 159,
160, 161, 162, 163, 164) x<-c(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
x<-c(-1071, -1072, -1073, -1074, -1075, -1074, -1073, -1072, -1071)
x<-c(1.5, 2.5, 3.5, 4.5, 5.5)
x<-c(157:164)
x
[1] 157 158 159 160 161 162 163 164
x<-c(10:1)
x
[1] 10 9 8 7 6 5 4 3 2 1
x<-append(c(-1071:-1075),c(-1074:-1071))
x
[1] -1071 -1072 -1073 -1074 -1075 -1074 -1073 -1072 -1071
x<-c(1.5:5.5)
x
[1] 1.5 2.5 3.5 4.5 5.5
- 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)
as.character(x)
[1] "0.15" "1.3" "3.45" "5.75"
as.integer(x)
[1] 0 1 3 5
- Create a vector y based on the requirements below:
- A sequence of 10 numbers from 20-11
- A sequence of odd numbers from 11-20
- A sequence of first twelve square number starting from 1.
- A sequence of first eleven exponential number of 2 starting from
1.
y<-c(20:11)
y
[1] 20 19 18 17 16 15 14 13 12 11
y<-seq(11,20,2)
y
[1] 11 13 15 17 19
y<-c(1:12)^2
y
[1] 1 4 9 16 25 36 49 64 81 100 121 144
y<-2^c(1:11)
y
[1] 2 4 8 16 32 64 128 256 512 1024 2048
- Create a vector z based on the requirements below:
- A sequence of 10 W
- A sequence of R R R S S S
- The first 5 alphabets in lower case
- A sequence of players from Player1 – Player10
z<-rep("w",10)
z
[1] "w" "w" "w" "w" "w" "w" "w" "w" "w" "w"
z<-rep(c("R","S"),each=3)
z
[1] "R" "R" "R" "S" "S" "S"
z<-letters[1:5]
z
[1] "a" "b" "c" "d" "e"
z<-paste0("Player",as.character(c(1:10)))
z
[1] "Player1" "Player2" "Player3" "Player4" "Player5" "Player6"
[7] "Player7" "Player8" "Player9" "Player10"
- Create vectors as below.
- Display the vector
- What is the total mark for Abu?
- Display the percentage for each student in two decimal places if the
total mark is 30.
Mtut1<-c("Ali"=15,"Abu"=17,"Ahmad"=10,"Bala"=8,"Chong"=19)
Mtut1
Ali Abu Ahmad Bala Chong
15 17 10 8 19
Mtut2<-c("Ali"=5,"Abu"=4,"Ahmad"=3,"Bala"=5,"Chong"=4)
Mtut2
Ali Abu Ahmad Bala Chong
5 4 3 5 4
Mtut1["Abu"]+Mtut2["Abu"]
Abu
21
round((Mtut1+Mtut2)/30*100,2)
Ali Abu Ahmad Bala Chong
66.67 70.00 43.33 43.33 76.67
- 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.
nums<-sample(c(51:100),10)
nums
[1] 77 76 59 75 55 53 64 61 52 80
even<-nums[c(nums%%2==0)]
even
[1] 76 64 52 80
- 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:
inchToCm<-function(num){
return(round(as.numeric(num)*2.54,2))
}
num=readline("Enter the length in inches: ")
2.1
paste(num," inches = ",inchToCm(num)," centimeters")
[1] "2.1 inches = 5.33 centimeters"
- 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:
discount<-function(price,rate){
return(paste("The price of the item after ",rate,"% discount is ",as.numeric(price)*as.numeric(rate)/100))
}
num=readline("Enter the price for the item: ")
50
discount(num,50)
[1] "The price of the item after 50 % discount is 25"
discount(num,30)
[1] "The price of the item after 30 % discount is 15"
discount(num,10)
[1] "The price of the item after 10 % discount is 5"
LS0tDQp0aXRsZTogIkxhYiAyIg0Kb3V0cHV0OiBodG1sX25vdGVib29rDQotLS0NCg0KDQpgYGB7cn0NCng8LTItMSoyDQ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