Programming for Data Science Lab 2 Basics in R

Group 7

R offers a wide variety of statistics-related libraries and provides a favorable environment for statistical computing and design. In addition, the R programming language gets used by many quantitative analysts as a programming tool since it’s useful for data importing and cleaning.

Including Code

  1. 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
  1. Shorten the notation of following vectors
x <- c(157, 158, 159, 160, 161, 162, 163, 164)
x <- c(157:164)
x
## [1] 157 158 159 160 161 162 163 164
x<-c(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
x <- c(10:1)
x
##  [1] 10  9  8  7  6  5  4  3  2  1
x <- c(-1071, -1072, -1073, -1074, -1075, -1074, -1073, -1072, -1071)
x <- c(-1071:-1075, -1074:-1071)
x
## [1] -1071 -1072 -1073 -1074 -1075 -1074 -1073 -1072 -1071
x <- c(1.5, 2.5, 3.5, 4.5, 5.5)
x <- c(1.5:5.5)
x
## [1] 1.5 2.5 3.5 4.5 5.5
  1. 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)
x_int <- as.integer(x)
class(x_int)
## [1] "integer"
typeof(x_int)
## [1] "integer"
x_ch <- as.character(x)
class(x_ch)
## [1] "character"
typeof(x_ch)
## [1] "character"
  1. Create a vector y based on the requirements below:
    # a. A sequence of 10 numbers from 20-11
x <- seq(20,11)
x
##  [1] 20 19 18 17 16 15 14 13 12 11
    # b. A sequence of odd numbers from 11-20

x <- seq(11,20, by=2)
x
## [1] 11 13 15 17 19
    # c. A sequence of first twelve square number starting from 1.
mysquare <- function(n){
  return(n^2)
}
mysquare(seq(1,12,by=1))
##  [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.
mysquare <- function(n){
  return(n^2)
}
mysquare(seq(1,11,by=2))
## [1]   1   9  25  49  81 121
  1. Create a vector z based on the requirements below:
   # a. A sequence of 10 W
x <- sample('W' , 10, replace = TRUE)
x
##  [1] "W" "W" "W" "W" "W" "W" "W" "W" "W" "W"
   # b. A sequence of R R R S S S
x <- rep(c('R', 'S'), each = 3)
x
## [1] "R" "R" "R" "S" "S" "S"
   # c. The first 5 alphabets in lower case

x <- letters[1:5]
x
## [1] "a" "b" "c" "d" "e"
   # d. A sequence of players from Player1 – Player10
print(paste0("Player", 1:10))
##  [1] "Player1"  "Player2"  "Player3"  "Player4"  "Player5"  "Player6" 
##  [7] "Player7"  "Player8"  "Player9"  "Player10"
  1. Create vectors as below.
  # a. Display the vector
Mtute1 <- c("Ali","Abu","Ahmad","Balal","Chong")
Mtutenum1 <- c(15,17,10,8,19)
Mtute2 <- c("Ali","Abu","Ahmad","Balal","Chong")
Mtutenum2 <- c(5,4,3,5,4)

sprintf("%s is %.0f",Mtute1[1:5],Mtutenum1[1:5])
## [1] "Ali is 15"   "Abu is 17"   "Ahmad is 10" "Balal is 8"  "Chong is 19"
sprintf("%s is %.0f",Mtute2[1:5],Mtutenum2[1:5])
## [1] "Ali is 5"   "Abu is 4"   "Ahmad is 3" "Balal is 5" "Chong is 4"
  # b. What is the total mark for Abu?
print(paste0('Abu Mark: ', Mtutenum1[2]+Mtutenum2[2]))
## [1] "Abu Mark: 21"
  # c. Display the percentage for each student in two decimal places if the total mark is 30.
sprintf("%s is %.2f %s",Mtute2[1:5],((Mtutenum1[1:5]+Mtutenum2[1:5])/30)*100,'%')
## [1] "Ali is 66.67 %"   "Abu is 70.00 %"   "Ahmad is 43.33 %" "Balal is 43.33 %"
## [5] "Chong is 76.67 %"
#Martix Way
Mtut <- matrix(nrow = 2,ncol = 5,byrow = TRUE)
Mtut[1,] = c(15,17,10,8,19) 
Mtut[2,] = c(5,4,3,5,4)
rownames(Mtut) <- (c("Mark_1","Mark_2"))
colnames(Mtut) <- (c("Ali","Abu","Ahmad","Balal", "Chong"))
Mtut
##        Ali Abu Ahmad Balal Chong
## Mark_1  15  17    10     8    19
## Mark_2   5   4     3     5     4
print(paste0('Abu: ',sum(Mtut[,2])))
## [1] "Abu: 21"