Professional Documents
Culture Documents
Lab Assignment 1: For in If Return Return
Lab Assignment 1: For in If Return Return
Vishal Jain
19103125
CSE G4
Question 1
[1] TRUE
Question 2
while(i >0){
print("Data Science")
i <- bitwShiftR(i,1)
}
Question 3
x <-5
while(x)
{
var = readline(prompt ="Enter any number : ") var = as.integer(var)
if(var == number){
print("Correct Answer..")
flush.console()
break
}
elseif(var < number){
print("Go Higher !")
flush.console()
}
else{
print("Go Lower !")
flush.console()
}
x=x-1
}
print("Better Luck next Time !")
}
[1] 53
Enter any number : 11
[1] "Go Higher !"
Enter any number : 25
[1] "Go Higher !"
Enter any number : 50
[1] "Go Higher !"
Enter any number : 52
[1] "Go Higher !"
Enter any number : 55
[1] "Go Lower !"
[1] "Better Luck next Time !"
Question 4
tries = readline(prompt ="Enter number of trials : "); tries = as.integer(tries)
x <- rbinom(tries,1,0.5)
res <-0
for(i in x)
{
res = res + i
}
ProbabilityHeads <- res/tries
ProabilityTrails <- (tries-res)/tries
print(paste("Probability of Heads = ",ProbabilityHeads," Probability of Tails = ",ProabilityTrails))
Question 5
solve <- function(numList){
return(numList[numList%%3!=0])
}
print(solve(c(1,2,3,5,6,9,8)))
[1] 1 2 5 8
Question 6
cereals <- structure(c(431.87, 284.33, 621.44, 95.01, 106.03, 102.45, 475.96, 297.85,
616.25, 102.93, 84.13, 117.74, 440.12, 313.61, 617.93, 109.33, 117.78, 131.14), .Dim =
c(6L, 3L), .Dimnames = list(c("United States", "India", "China", "Indonesia", "Braziiil Ole
Ole", "Russian Federation"), c("2015", "2016", "in 2017")))
print(cereals)
is.matrix(cereals)
dim(cereals)
[1] TRUE
[1] 6 3
Question 7
Name <
c("Anastasia","Dima","Katherine","James","Emily","Michael","Matthew"," Laura","Kevin","Jonas")
Score <- c(11.5,8.0,15.5,1.0,5.5,19.0,12.5,10.5,7.0,18)
Attempts <- c(3,3,NA,2,NA,NA,1,1,2,NA)
Qualify <- c("yes","no","yes","no","no","yes","yes","no","no","yes") Gender <-
c("F","F","F","M","F","M","M","F","M","M")
Age <- c(18,17,NA,NA,19,17,18,17,19,19)
#Creating dataFrame
#Deleting Column Gender
df <- data.frame(Name,Score,Attempts,Qualify,Gender,Age)
print(df)
cat("\n")
df <- subset (df, select =-Gender)
print(df)
cat("\n")
#Increment score by 1
cat("\n")
df$Score <- df$Score +1
df
[1] 10 5
[1] 10
[1] 5
Question 8
summary(iris)
#display last 10 records of dataset
last10Rows = tail(iris, n =10)
print(last10Rows)
[1] 150 5
[1] 150
[1] 5