Assignment 2 output 229010
Assignment 2 output 229010
>
>x=1
[1] 2
>
>x=1
[1] 2
[1] 4
[1] 5
>
>
> #Q2 The %% operator returns the modulus (remainder) of a division operation.
>
+ {
+ 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
>
> data(mtcars)
>
>
> mtcars[mtcars$cyl == 6, ]
>
> mtcars[order(mtcars$mpg), ]
>
> table(mtcars$cyl)
4 6 8
11 7 14
>
> # e) Using tapply() function, calculate the mean and sum of hp, respectively for diffe
cylinders
4 6 8
4 6 8
909 856 2929
>
> #Q4
>
> #a
> cities = c("Pune", "Mumbai", "Indore", "Bhopal", "Agra", "Jaipur", "Chandigarh", "Nag
"Kanpur")
>
> #b
>
> #c
>
> #d
> print(Data)
> #e
> e=Data[Data$malepopulation>50,]
> print(e)
>
>
> #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)
4 Siya 39 No North
6 Hardit 54 No East
8 Abeer NA No South
11 Shekhar 28 No East
>
> #a
> head(Voters, n = 6)
3 Ramya 34 No East
4 Siya 39 No North
6 Hardit 54 No East
>
> #b
> print(Voters)
3 Ramya 34 No East
4 Siya 39 No North
6 Hardit 54 No East
8 Abeer NA No South
11 Shekhar 28 No East
>
> #c
> print(Voters)
3 Ramya 34 No East
4 Siya 39 No North
6 Hardit 54 No East
8 Abeer NA No South
11 Shekhar 28 No East
>
> #d
> print(subset)
Name Voter_ID
3 Ramya No
5 Devagya Yes
>
> #e
> 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)
>
> print(constituency_counts)
4 3 4 1
>
>
> print(Voters_1)
3 Ramya 34 No East
4 Siya 39 No North
6 Hardit 54 No East
8 Abeer NA No South
11 Shekhar 28 No East
>
> #Q6
>
+ {
+ if (percentage >= 90) {return("A")}
+ else {return("E")}
+}
>
> print(grade)
[1] "A"
>
> #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"
>
>
>
>a=1
>b=2
>c=3
>
> greatest = a
>{
+}
> print(greatest)
[1] 3
>
> #Q9
>
> number = 5
[1] 5
[1] 10
[1] 15
[1] 20
[1] 25
[1] 30
[1] 35
[1] 40
[1] 45
[1] 50
>
> #Q10
>
> even=0
>
+{
+ if (o%%2==0) {even=even+o}
+}
> print(even)
[1] 240