0% found this document useful (0 votes)
17 views17 pages

Assignment 2 output 229010

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views17 pages

Assignment 2 output 229010

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

#Q1

>

>x=1

> while (x < 5) {x = x+1; if (x == 3) break; print(x); }

[1] 2

>

>x=1

> while (x < 5) {x = x+1; if (x == 3) next; print(x); }

[1] 2

[1] 4

[1] 5

>

>

> #Q2 The %% operator returns the modulus (remainder) of a division operation.

> #For instance, 5 %% 2 would return 1, as the remainder of 5 divided by 2 is 1.

>

> for (i in 1:100)

+ {

+ if (i %% 3 == 0 && i %% 5 == 0) { print("ThisThat")}

+ else if (i %% 3 == 0) { print("This") }

+ else if (i %% 5 == 0) { print("That")}

+ else {print(i)}

+}

[1] 1

[1] 2

[1] "This"

[1] 4

[1] "That"
[1] "This"

[1] 7

[1] 8

[1] "This"

[1] "That"

[1] 11

[1] "This"

[1] 13

[1] 14

[1] "ThisThat"

[1] 16

[1] 17

[1] "This"

[1] 19

[1] "That"

[1] "This"

[1] 22

[1] 23

[1] "This"

[1] "That"

[1] 26

[1] "This"

[1] 28

[1] 29

[1] "ThisThat"

[1] 31

[1] 32

[1] "This"
[1] 34

[1] "That"

[1] "This"

[1] 37

[1] 38

[1] "This"

[1] "That"

[1] 41

[1] "This"

[1] 43

[1] 44

[1] "ThisThat"

[1] 46

[1] 47

[1] "This"

[1] 49

[1] "That"

[1] "This"

[1] 52

[1] 53

[1] "This"

[1] "That"

[1] 56

[1] "This"

[1] 58

[1] 59

[1] "ThisThat"

[1] 61
[1] 62

[1] "This"

[1] 64

[1] "That"

[1] "This"

[1] 67

[1] 68

[1] "This"

[1] "That"

[1] 71

[1] "This"

[1] 73

[1] 74

[1] "ThisThat"

[1] 76

[1] 77

[1] "This"

[1] 79

[1] "That"

[1] "This"

[1] 82

[1] 83

[1] "This"

[1] "That"

[1] 86

[1] "This"

[1] 88

[1] 89
[1] "ThisThat"

[1] 91

[1] 92

[1] "This"

[1] 94

[1] "That"

[1] "This"

[1] 97

[1] 98

[1] "This"

[1] "That"

>

> #Q3

>

> # Load the mtcars dataset

> data(mtcars)

>

> # a) Display the columns from mpg to hp

> mtcars[, c("mpg", "cyl", "disp", "hp")]

mpg cyl disp hp

Mazda RX4 21.0 6 160.0 110

Mazda RX4 Wag 21.0 6 160.0 110

Datsun 710 22.8 4 108.0 93

Hornet 4 Drive 21.4 6 258.0 110

Hornet Sportabout 18.7 8 360.0 175

Valiant 18.1 6 225.0 105

Duster 360 14.3 8 360.0 245

Merc 240D 24.4 4 146.7 62


Merc 230 22.8 4 140.8 95

Merc 280 19.2 6 167.6 123

Merc 280C 17.8 6 167.6 123

Merc 450SE 16.4 8 275.8 180

Merc 450SL 17.3 8 275.8 180

Merc 450SLC 15.2 8 275.8 180

Cadillac Fleetwood 10.4 8 472.0 205

Lincoln Continental 10.4 8 460.0 215

Chrysler Imperial 14.7 8 440.0 230

Fiat 128 32.4 4 78.7 66

Honda Civic 30.4 4 75.7 52

Toyota Corolla 33.9 4 71.1 65

Toyota Corona 21.5 4 120.1 97

Dodge Challenger 15.5 8 318.0 150

AMC Javelin 15.2 8 304.0 150

Camaro Z28 13.3 8 350.0 245

Pontiac Firebird 19.2 8 400.0 175

Fiat X1-9 27.3 4 79.0 66

Porsche 914-2 26.0 4 120.3 91

Lotus Europa 30.4 4 95.1 113

Ford Pantera L 15.8 8 351.0 264

Ferrari Dino 19.7 6 145.0 175

Maserati Bora 15.0 8 301.0 335

Volvo 142E 21.4 4 121.0 109

>

> # b) Display those records from “mtcars” dataset whose cylinder is 6

> mtcars[mtcars$cyl == 6, ]

mpg cyl disp hp drat wt qsec vs am gear carb


Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4

Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4

Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1

Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1

Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4

Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4

Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6

>

> # c) Display the records on the basis of ascending order of mpg

> mtcars[order(mtcars$mpg), ]

mpg cyl disp hp drat wt qsec vs am gear carb

Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4

Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4

Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4

Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4

Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4

Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8

Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3

AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2

Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2

Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4

Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3

Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3

Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4

Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1

Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2

Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4

Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2


Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6

Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4

Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4

Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1

Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2

Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1

Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1

Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2

Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2

Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2

Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1

Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2

Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2

Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1

Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1

>

> # d) Count the number of records based on different categories of cylinder

> table(mtcars$cyl)

4 6 8

11 7 14

>

> # e) Using tapply() function, calculate the mean and sum of hp, respectively for diffe
cylinders

> tapply(mtcars$hp, mtcars$cyl, mean)

4 6 8

82.63636 122.28571 209.21429

> tapply(mtcars$hp, mtcars$cyl, sum)

4 6 8
909 856 2929

>

> #Q4

>

> #a

> cities = c("Pune", "Mumbai", "Indore", "Bhopal", "Agra", "Jaipur", "Chandigarh", "Nag
"Kanpur")

>

> #b

> malepopulation = c(1000000, 2000000, 500000, 3000000, 1500000, 2500000, 1200


4000000, 900000)

>

> #c

> femalepopulation = c(900000, 1800000, 450000, 2800000, 1400000, 2300000, 110


3800000, 850000)

>

> #d

> Data = data.frame(cities, malepopulation, femalepopulation)

> print(Data)

cities malepopulation femalepopulation

1 Pune 1000000 900000

2 Mumbai 2000000 1800000

3 Indore 500000 450000

4 Bhopal 3000000 2800000

5 Agra 1500000 1400000

6 Jaipur 2500000 2300000

7 Chandigarh 1200000 1100000

8 Nagpur 4000000 3800000

9 Kanpur 900000 850000


>

> #e

> e=Data[Data$malepopulation>50,]

> print(e)

cities malepopulation femalepopulation

1 Pune 1000000 900000

2 Mumbai 2000000 1800000

3 Indore 500000 450000

4 Bhopal 3000000 2800000

5 Agra 1500000 1400000

6 Jaipur 2500000 2300000

7 Chandigarh 1200000 1100000

8 Nagpur 4000000 3800000

9 Kanpur 900000 850000

>

>

> #Q5

>

> Name = c("Rashmi", "Khushboo", "Ramya", "Siya", "Devagya", "Hardit", "Puneet", "A
"Ameya", "Diya", "Shekhar", "Rubani")

> Age = c(45, 21, 34, 39, NA, 54, 67, NA, 26, 22, 28, 35)

> Voter_ID = c("Yes", "Yes", "No", "No", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes

> Constituency = c("North", "South", "East", "North", "South", "East", "West", "South",
"North", "East", "East")

> Voters=data.frame(Name,Age,Voter_ID,Constituency)

> print(Voters)

Name Age Voter_ID Constituency

1 Rashmi 45 Yes North

2 Khushboo 21 Yes South


3 Ramya 34 No East

4 Siya 39 No North

5 Devagya NA Yes South

6 Hardit 54 No East

7 Puneet 67 Yes West

8 Abeer NA No South

9 Ameya 26 Yes South

10 Diya 22 Yes North

11 Shekhar 28 No East

12 Rubani 35 Yes East

>

> #a

> head(Voters, n = 6)

Name Age Voter_ID Constituency

1 Rashmi 45 Yes North

2 Khushboo 21 Yes South

3 Ramya 34 No East

4 Siya 39 No North

5 Devagya NA Yes South

6 Hardit 54 No East

>

> #b

> North_Voters = Voters[Voters$Constituency == "North" & Voters$Age > 25, ]

> print(Voters)

Name Age Voter_ID Constituency

1 Rashmi 45 Yes North

2 Khushboo 21 Yes South

3 Ramya 34 No East
4 Siya 39 No North

5 Devagya NA Yes South

6 Hardit 54 No East

7 Puneet 67 Yes West

8 Abeer NA No South

9 Ameya 26 Yes South

10 Diya 22 Yes North

11 Shekhar 28 No East

12 Rubani 35 Yes East

>

> #c

> mean_age = mean(Age, na.rm = TRUE)

> Age = replace(Age,is.na(Age),mean_age)

> print(Voters)

Name Age Voter_ID Constituency

1 Rashmi 45 Yes North

2 Khushboo 21 Yes South

3 Ramya 34 No East

4 Siya 39 No North

5 Devagya NA Yes South

6 Hardit 54 No East

7 Puneet 67 Yes West

8 Abeer NA No South

9 Ameya 26 Yes South

10 Diya 22 Yes North

11 Shekhar 28 No East

12 Rubani 35 Yes East

>
> #d

> subset = Voters[c(3, 5), c(1, 3)]

> print(subset)

Name Voter_ID

3 Ramya No

5 Devagya Yes

>

> #e

> Age_2030 = Age+7

> print(Age_2030)

[1] 52.0 28.0 41.0 46.0 44.1 61.0 74.0 44.1 33.0 29.0 35.0 42.0

> Voters_1=cbind(Voters,Age_2030)

> print(Voters_1)

Name Age Voter_ID Constituency Age_2030

1 Rashmi 45 Yes North 52.0

2 Khushboo 21 Yes South 28.0

3 Ramya 34 No East 41.0

4 Siya 39 No North 46.0

5 Devagya NA Yes South 44.1

6 Hardit 54 No East 61.0

7 Puneet 67 Yes West 74.0

8 Abeer NA No South 44.1

9 Ameya 26 Yes South 33.0

10 Diya 22 Yes North 29.0

11 Shekhar 28 No East 35.0

12 Rubani 35 Yes East 42.0

>

> #f Count the number of records based on “Constituency”.


> constituency_counts = table(Voters$Constituency)

> print(constituency_counts)

East North South West

4 3 4 1

>

> #g Delete the column “Age_2030” by assigning NULL to it

>

> Voters_1$Age_2030 = NULL

> print(Voters_1)

Name Age Voter_ID Constituency

1 Rashmi 45 Yes North

2 Khushboo 21 Yes South

3 Ramya 34 No East

4 Siya 39 No North

5 Devagya NA Yes South

6 Hardit 54 No East

7 Puneet 67 Yes West

8 Abeer NA No South

9 Ameya 26 Yes South

10 Diya 22 Yes North

11 Shekhar 28 No East

12 Rubani 35 Yes East

>

> #Q6

>

> grade_calculator = function(percentage)

+ {
+ if (percentage >= 90) {return("A")}

+ else if (percentage >= 80) {return("B")}

+ else if (percentage >= 60) {return("C")}

+ else if (percentage >= 33) {return("D")}

+ else {return("E")}

+}

>

> grade = grade_calculator(95)

> print(grade)

[1] "A"

>

> #Q7 (DON'T USE X HEREE AS ALREADY USED)

> #a

> switch(2,"red","green","blue");

[1] "green"

>x

[1] 5

>

> #b

> switch(4,"red","green","blue");

> x

[1] 5

> #c

> x = switch(0,"red","green","blue");

>x

NULL

>

> #d
> x = switch(1,"red","green","blue");

> x

[1] "red"

>

>

>

> #Q8 USING A,B,C INSTEAD OF X,Y,Z

>a=1

>b=2

>c=3

>

> greatest = a

>{

+ if (b > greatest) { greatest = b}

+ if (c > greatest) { greatest = c}

+}

> print(greatest)

[1] 3

>

> #Q9

>

> number = 5

> for (u in 1:10) { print(number * u)}

[1] 5

[1] 10

[1] 15

[1] 20

[1] 25
[1] 30

[1] 35

[1] 40

[1] 45

[1] 50

>

> #Q10

>

> even=0

>

> for (o in 1:30)

+{

+ if (o%%2==0) {even=even+o}

+}

> print(even)

[1] 240

You might also like