Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

LAB ASSIGNMENT 1

Vishal Jain
19103125
CSE G4

Question 1

isLucky <- function(numList){


for(num in numList){
if(num %%7==0){
return(TRUE)
}
}
return(FALSE)
}

empId = c(1, 2, 7, 14)


isLucky(empId)

[1] TRUE

Question 2
while(i >0){
print("Data Science")
i <- bitwShiftR(i,1)
}

[1] "Data Science"


[1] "Data Science"
[1] "Data Science"
[1] "Data Science"
[1] "Data Science"
[1] "Data Science"
[1] "Data Science"
[1] "Data Science"
[1] "Data Science"
[1] "Data Science"
[1] "Data Science"

Question 3

game <- function(number){

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 !")
}

number <- round(runif(1, min=1, max=100)) print(number)


game(number)

[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))

Enter number of trials : 9999999


[1] "Probability of Heads = 0.499706349970635 Probability of Tails = 0.500293650029365"

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)

rowNames <- rownames(cereals)


colNames <- colnames(cereals)
print(paste("Row names : ", rowNames))
print(paste("column names :",colNames))

#Changing column names


colnames(cereals)[colnames(cereals) =="in 2017"] <-"2017" print(cereals)

#Changing row names


rownames(cereals)[rownames(cereals) =="Braziiil Ole\nOle" ] <-"Brazil"
print(cereals)
2015 2016 in 2017 United
States 431.87 475.96 440.12 India 284.33
297.85 313.61 China 621.44 616.25 617.93
Indonesia 95.01 102.93 109.33 Braziiil Ole\nOle
106.03 84.13 117.78 Russian Federation 102.45 117.74
131.14

[1] TRUE

[1] 6 3

[1] "Row names : United States" "Row names : India"

[3] "Row names : China" "Row names : Indonesia"

[5] "Row names : Braziiil Ole\nOle" "Row names : Russian


Federation"
[1] "column names : 2015" "column names : 2016" "column names : in 2017"
2015 2016 2017
United States 431.87 475.96 440.12
India 284.33 297.85 313.61
China 621.44 616.25 617.93
Indonesia 95.01 102.93 109.33
Braziiil Ole\nOle 106.03 84.13 117.78
Russian Federation 102.45 117.74 131.14
2015 2016 2017
United States 431.87 475.96 440.12
India 284.33 297.85 313.61
China 621.44 616.25 617.93
Indonesia 95.01 102.93 109.33
Brazil 106.03 84.13 117.78
Russian Federation 102.45 117.74 131.14

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")

#frequency of NA each Column


sapply(df, function(x) sum(is.na(x))) cat("\n")

#Replacing NA with mean


df$Attempts[is.na(df$Attempts)]<-mean(df$Attempts,na.rm=TRUE) df

#Increment score by 1
cat("\n")
df$Score <- df$Score +1
df

#Create Percentage column and delete score column cat("\n")


df$percentage <- round(df$Score/20*100)
df <- subset (df, select =-Score)
df

#Displaying data having conditions


cat("\n")
newDf = subset(df, Age <19& Qualify =="yes") newDf

#Sort dataframe on the basis of Percentage column cat("\n")


df <-df[order(df$percentage),]
df

#print dimension , rows and columns


cat("\n")
dim(df)
nrow(df)
ncol(df)
Name Score Attempts Qualify Gender Age 1 Anastasia
11.5 3 yes F 18 2 Dima 8.0 3 no F 17 3
Katherine 15.5 NA yes F NA 4 James 1.0 2 no
M NA 5 Emily 5.5 NA no F 19 6 Michael 19.0
NA yes M 17 7 Matthew 12.5 1 yes M 18 8
Laura 10.5 1 no F 17 9 Kevin 7.0 2 no M
19 10 Jonas 18.0 NA yes M 19

Name Score Attempts Qualify Age 1


Anastasia 11.5 3 yes 18 2 Dima 8.0 3
no 17 3 Katherine 15.5 NA yes NA 4 James
1.0 2 no NA 5 Emily 5.5 NA no 19 6
Michael 19.0 NA yes 17 7 Matthew 12.5 1
yes 18 8 Laura 10.5 1 no 17 9 Kevin 7.0
2 no 19 10 Jonas 18.0 NA yes 19

Name Score Attempts Qualify Age 0 0 4 0


2

Name Score Attempts Qualify Age 1 Anastasia


11.5 3 yes 18 2 Dima 8.0 3 no 17 3
Katherine 15.5 2 yes NA 4 James 1.0 2 no
NA 5 Emily 5.5 2 no 19 6 Michael 19.0 2
yes 17 7 Matthew 12.5 1 yes 18 8 Laura
10.5 1 no 17 9 Kevin 7.0 2 no 19 10
Jonas 18.0 2 yes 19

Name Score Attempts Qualify Age 1 Anastasia


12.5 3 yes 18 2 Dima 9.0 3 no 17 3
Katherine 16.5 2 yes NA 4 James 2.0 2 no
NA 5 Emily 6.5 2 no 19 6 Michael 20.0 2
yes 17
7 Matthew 13.5 1 yes 18 8 Laura 11.5 1
no 17 9 Kevin 8.0 2 no 19 10 Jonas 19.0
2 yes 19

Name Attempts Qualify Age percentage 1 Anastasia 3


yes 18 62
2 Dima 3 no 17 45
3 Katherine 2 yes NA 82
4 James 2 no NA 10
5 Emily 2 no 19 32
6 Michael 2 yes 17 100
7 Matthew 1 yes 18 68
8 Laura 1 no 17 57
9 Kevin 2 no 19 40
10 Jonas 2 yes 19 95

Name Attempts Qualify Age percentage 1 Anastasia 3


yes 18 62
6 Michael 2 yes 17 100
7 Matthew 1 yes 18 68

Name Attempts Qualify Age percentage 4 James 2


no NA 10
5 Emily 2 no 19 32
9 Kevin 2 no 19 40
2 Dima 3 no 17 45
8 Laura 1 no 17 57
1 Anastasia 3 yes 18 62
7 Matthew 1 yes 18 68
3 Katherine 2 yes NA 82
10 Jonas 2 yes 19 95
6 Michael 2 yes 17 100

[1] 10 5

[1] 10

[1] 5

Question 8
summary(iris)
#display last 10 records of dataset
last10Rows = tail(iris, n =10)
print(last10Rows)

#display 5th row dataset


print(iris[5,])

#Display 2nd and 3rd column of Dataset iris[, c(2,3)]

#Display subset having Vercicolor


newIris = subset(iris,Species =="versicolor") newIris

#Show structure of iris dataset


dim(iris)
nrow(iris)
ncol(iris)

Sepal.Length Sepal.Width Petal.Length Petal.Width Min. :4.300 Min. :2.000


Min. :1.000 Min. :0.100 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
Median :5.800 Median :3.000 Median :4.350 Median :1.300 Mean :5.843 Mean :3.057
Mean :3.758 Mean :1.199 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500 Species
setosa :50
versicolor:50
virginica :50

Sepal.Length Sepal.Width Petal.Length Petal.Width Species 141 6.7 3.1


5.6 2.4 virginica 142 6.9 3.1 5.1 2.3 virginica 143 5.8 2.7
5.1 1.9 virginica 144 6.8 3.2 5.9 2.3 virginica 145 6.7 3.3
5.7 2.5 virginica 146 6.7 3.0 5.2 2.3 virginica 147 6.3 2.5
5.0 1.9 virginica 148 6.5 3.0 5.2 2.0 virginica 149 6.2 3.4
5.4 2.3 virginica 150 5.9 3.0 5.1 1.8 virginica Sepal.Length
Sepal.Width Petal.Length Petal.Width Species 5 5 3.6 1.4 0.2 setosa
Sepal.Width Petal.Length
1 3.5 1.4
2 3.0 1.4
3 3.2 1.3
4 3.1 1.5
5 3.6 1.4
6 3.9 1.7
7 3.4 1.4
8 3.4 1.5
9 2.9 1.4
10 3.1 1.5
11 3.7 1.5
12 3.4 1.6
13 3.0 1.4
14 3.0 1.1
15 4.0 1.2
16 4.4 1.5
17 3.9 1.3
18 3.5 1.4
19 3.8 1.7
20 3.8 1.5
21 3.4 1.7
22 3.7 1.5
23 3.6 1.0
24 3.3 1.7
25 3.4 1.9
26 3.0 1.6
27 3.4 1.6
28 3.5 1.5
29 3.4 1.4
30 3.2 1.6

⋮ ⋮121 3.2 5.7
122 2.8 4.9
123 2.8 6.7
124 2.7 4.9
125 3.3 5.7
126 3.2 6.0
127 2.8 4.8
128 3.0 4.9
129 2.8 5.6
130 3.0 5.8
131 2.8 6.1
132 3.8 6.4
133 2.8 5.6
134 2.8 5.1
135 2.6 5.6
136 3.0 6.1
137 3.4 5.6
138 3.1 5.5
139 3.0 4.8
140 3.1 5.4
141 3.1 5.6
142 3.1 5.1
143 2.7 5.1
144 3.2 5.9
145 3.3 5.7
146 3.0 5.2
147 2.5 5.0
148 3.0 5.2
149 3.4 5.4
150 3.0 5.1

Sepal.Length Sepal.Width Petal.Length Petal.Width Species 51 7.0 3.2 4.7


1.4 versicolor 52 6.4 3.2 4.5 1.5 versicolor 53 6.9 3.1 4.9
1.5 versicolor 54 5.5 2.3 4.0 1.3 versicolor 55 6.5 2.8 4.6
1.5 versicolor 56 5.7 2.8 4.5 1.3 versicolor 57 6.3 3.3 4.7
1.6 versicolor 58 4.9 2.4 3.3 1.0 versicolor 59 6.6 2.9 4.6
1.3 versicolor 60 5.2 2.7 3.9 1.4 versicolor 61 5.0 2.0 3.5
1.0 versicolor 62 5.9 3.0 4.2 1.5 versicolor 63 6.0 2.2 4.0
1.0 versicolor 64 6.1 2.9 4.7 1.4 versicolor 65 5.6 2.9 3.6
1.3 versicolor 66 6.7 3.1 4.4 1.4 versicolor 67 5.6 3.0 4.5
1.5 versicolor 68 5.8 2.7 4.1 1.0 versicolor 69 6.2 2.2 4.5
1.5 versicolor 70 5.6 2.5 3.9 1.1 versicolor 71 5.9 3.2 4.8
1.8 versicolor 72 6.1 2.8 4.0 1.3 versicolor 73 6.3 2.5 4.9
1.5 versicolor 74 6.1 2.8 4.7 1.2 versicolor 75 6.4 2.9 4.3
1.3 versicolor 76 6.6 3.0 4.4 1.4 versicolor 77 6.8 2.8 4.8
1.4 versicolor 78 6.7 3.0 5.0 1.7 versicolor 79 6.0 2.9 4.5
1.5 versicolor 80 5.7 2.6 3.5 1.0 versicolor 81 5.5 2.4 3.8
1.1 versicolor 82 5.5 2.4 3.7 1.0 versicolor 83 5.8 2.7 3.9
1.2 versicolor 84 6.0 2.7 5.1 1.6 versicolor 85 5.4 3.0 4.5
1.5 versicolor 86 6.0 3.4 4.5 1.6 versicolor
87 6.7 3.1 4.7 1.5 versicolor 88 6.3 2.3 4.4 1.3
versicolor 89 5.6 3.0 4.1 1.3 versicolor 90 5.5 2.5 4.0 1.3
versicolor 91 5.5 2.6 4.4 1.2 versicolor 92 6.1 3.0 4.6 1.4
versicolor 93 5.8 2.6 4.0 1.2 versicolor 94 5.0 2.3 3.3 1.0
versicolor 95 5.6 2.7 4.2 1.3 versicolor 96 5.7 3.0 4.2 1.2
versicolor 97 5.7 2.9 4.2 1.3 versicolor 98 6.2 2.9 4.3 1.3
versicolor 99 5.1 2.5 3.0 1.1 versicolor 100 5.7 2.8 4.1 1.3
versicolor

[1] 150 5

[1] 150

[1] 5

You might also like