Asim
Asim
Asim
(i) Express the data of birth rates and death rates per thousand of some countries in the R-
studio. Execute their columns by giving the commands separately. Also, draw the
histograms and bar charts to differentiate them?
Solution:
A = data.frame(Country=c("India","Japan","Germany","Egypt","Australia","New
Zealand","France","Russia"),Birth_Rate=c(33,32,16,44,20,18,21,38),Death_Rate=c(24,19,1
0,24,9,8,16,16))
v = c(33,32,16,44,20,18,21,38)
x = c(24,19,10,24,9,8,16,16)
v = c(33,32,16,44,20,18,21,38)
y = c("India","Japan","Germany","Egypt","Australia","New Zealand","France","Russia")
x = c(24,19,10,24,9,8,16,16)
y = c("India","Japan","Germany","Egypt","Australia","New Zealand","France","Russia")
barplot(x,names.arg = y,xlab = "Country",ylab = "Death_Rate",col = "yellow",main =
"Death_Rate Bar Chart",border = "red")
(ii) The table given below provides the detail of monthly expenditure of three families.
Express it with the help of data frame. Also draw the histograms and bar charts to
differentiate them?
Solution:
B = data.frame(Items_of_Expenditure=c("Food
Articles","Clothing","Recreation","Education","Rent","Miscellaneous"),Family_A_Rs.=c(43
,18,3,5,10,6),Family_B_Rs.=c(87,17,10,9,21,15),Family_C_Rs.=c(120,25,12,15,17,11))
Family_A_Rs.=c(43,18,3,5,10,6)
Family_C_Rs.=c(120,25,12,15,17,11)
Family_A_Rs.=c(43,18,3,5,10,6)
Items_of_Expenditure=c("Food
Articles","Clothing","Recreation","Education","Rent","Miscellaneous")
Family_B_Rs.=c(87,17,10,9,21,15)
Items_of_Expenditure=c("Food
Articles","Clothing","Recreation","Education","Rent","Miscellaneous")
Family_C_Rs.=c(120,25,12,15,17,11)
Items_of_Expenditure=c("Food
Articles","Clothing","Recreation","Education","Rent","Miscellaneous")
barplot(Family_C_Rs.,names.arg = Items_of_Expenditure,xlab = "Items of
Expenditure",ylab = "Family C Expenditure",col = "yellow",main = "Family_C_Expenditure
Bar Chart",border = "red")
Q3: Create a vector of 1st ten natural number, find their square with the help of for loop .
loop terminates as the number is greater than 6.
Solution:
x = c(1:10)
for(i in x){
if(i>6)
break
print(i^2)
Q4: From the 1st ten natural number, create its vector, then from its vector, print its values at
more than 5 with the help of while loop.
Solution:
x = c(1:10)
i=1
while(i<11)
if(x[i]>5)
{
print(x[i])
i=i+1
Q5: Apply R programming or minit tab and make work for Fibonacci sequence, which is the
integer sequence of 0,1,1,2,3,5,8,…
Solution:
n1 = 0
n2 = 1
count = 2
} else{
if(total_terms == 1){
print("Fibonacci sequence")
print(n1)
}else{
print("Fibonacci sequence")
print(n1)
print(n2)
while(count<total_terms){
nxt = n1+n2
print(nxt)
n1 = n2
n2=nxt
count=count +1
Q6. A vector is given by V= [5, 17, -3, 8, 0, -7, 12, 15, 20, -6, 6, 4, -7, 16]. Write a
program as a script file that doubles the elements that are positive and are divisible
by 3 or 5, and, raises to the power of 3 the elements that are negative but greater
than -5.
Solution:
x = c(5,17,-3,8,0,-7,12,15,20,-6,6,4,-7,16)
for (i in x) {
print(i*2)
print(i^3)
}
}
Q7 Write a program in a script file that creates an n x m matrix with elements that
have the following values. The value of each element in the first row is the number of the
column. The value of each element in the first column is the number of the row. The rest of
the elements each has a value equal to the sum of the element above it and the element to the
left. When executed, the program asks the user to enter values for n and m.
solution:
i=1
while(i<=rows)
j=1
while(j<=columns)
if(i==1 | j==1)
if(i==1)
m[i,j]=columns
else
{
m[i,j]=rows
else
sum=0
h=1
while(h<i)
sum=sum+m[h,j]
h=h+1
h=1
while(h<j)
sum=sum+m[i,h]
h=h+1
m[i,j]=sum
j=j+1
}
i=i+1
print(m)
Q1 Initialize a natural by your one or two-digit roll number. Increase it by one unit.
then print those numbers where each one is obtained by multiplying a number by its
immediate next one. (i) Use the while loop and cat command for the next ten natural
numbers.
(ii) then call a user defined function (defined below) in the console window to find the
sum of their components like
2019-ME-01
1st com=x=2+0+1+9=12
2nd com=y=M+E=13+5=18
Code
fibwhile<-function(n){
+ if(n==0) return(0)
+ f<-c(1,2)
+ while(n+1>length(f)){
+ l<-length(f)
+ x<-f[l]*f[l-1]
+ f<-c(f,x)
+ }
+ f
+}
>
> fibwhile(1)
[1] 1 2
> fibwhile(2)
[1] 1 2 2
> fibwhile(3)
[1] 1 2 2 4
> fibwhile(4)
[1] 1 2 2 4 8
> fibwhile(5)
[1] 1 2 2 4 8 32
> fibwhile(6)
[1] 1 2 2 4 8 32 256
> fibwhile(7)
[1] 1 2 2 4 8 32 256 8192
library(readxl)
> View(LungCapData)
> str(LungCapData)
$ Height : num [1:725] 62.1 74.7 69.7 71 56.9 58.7 63.3 70.4 70.5 59.2 ...
>
>
>
> min(LungCapData$LungCap)
[1] 0.507
> max(LungCapData$LungCap)
[1] 14.675
> median(LungCapData$LungCap)
[1] 8
> mode(LungCapData$LungCap)
[1] "numeric"
> quantile(LungCapData$LungCap)
> quantile(LungCapData$LungCap,0)
0%
0.507
> quantile(LungCapData$LungCap,25)
> quantile(LungCapData$LungCap,.25)
25%
6.15
>
>
>
>
>
>
>
>
> min(LungCapData$LungCap)
[1] 0.507
> max(LungCapData$LungCap)
[1] 14.675
> median(LungCapData$LungCap)
[1] 8
> mode(LungCapData$LungCap)
[1] "numeric"
> quantile(LungCapData$LungCap)
0% 25% 50% 75% 100%
> quantile(LungCapData$LungCap,0)
0%
0.507
> quantile(LungCapData$LungCap,.25)
25%
6.15
> quantile(LungCapData$LungCap,.50)
50%
> quantile(LungCapData$LungCap,0.75)
75%
9.8
> quantile(LungCapData$LungCap,1)
100%
14.675
>
>
> min(LungCapData$Age)
[1] 3
> max(LungCapData$Age)
[1] 19
> median(LungCapData$Age)
[1] 13
> mode(LungCapData$Age)
[1] "numeric"
> quantile(LungCapData$Age)
3 9 13 15 19
> quantile(LungCapData$Age,0)
0%
> quantile(LungCapData$Age,0.25)
25%
> quantile(LungCapData$Age,0.50)
50%
13
> quantile(LungCapData$Age,0.75)
75%
15
> quantile(LungCapData$Age,1)
100%
19
>
>
>
> min(LungCapData$Height)
[1] 45.3
> max(LungCapData$Height)
[1] 81.8
> median(LungCapData$Height)
[1] 65.4
> mode(LungCapData$Height)
[1] "numeric"
> quantile(LungCapData$Height)
> quantile(LungCapData$Height,0)
0%
45.3
> quantile(LungCapData$Height,0.25)
25%
59.9
> quantile(LungCapData$Height,0.50)
50%
65.4
> quantile(LungCapData$Height,0.75)
75%
70.3
> quantile(LungCapData$Height,1)
100%
81.8
>
>
>
>
> fivenum(LungCapData)
Error:
Warning message:
>
>
>
>
> fivenum(LungCapData$LungCap)
> fivenum(LungCapData$Age)
[1] 3 9 13 15 19
> fivenum(LungCapData$Height)
> fivenum(LungCapData$Smoke)
>
>
>
>
>
> class(LungCapData$LungCap)
[1] "numeric"
> class(LungCapData$Age)
[1] "numeric"
> class(LungCapData$Height)
[1] "numeric"
> class(LungCapData$Smoke)
[1] "character"
> class(LungCapData$Gender)
[1] "character"
> class(LungCapData$Caesarean)
[1] "character"
> typeof(LungCapData$LungCap)
[1] "double"
> typeof(LungCapData$Age)
[1] "double"
> typeof(LungCapData$Height)
[1] "double"
> typeof(LungCapData$Smoke)
[1] "character"
> typeof(LungCapData$Gender)
[1] "character"
> typeof(LungCapData$Caesarean)
[1] "character"
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> frequency(LungCapData$Age)
[1] 1
> frequency(LungCapData$Height)
[1] 1
> table(LungCapData)
Age
LungCap 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0.507 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1.025 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1.125 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1.175 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1.325 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1.45 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1.575 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1.625 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1.675 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1.775 0 0 0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 0 0 0 0 0 0 0 0
3.65 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3.675 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3.7 0000000 0 0 0 0 0 0 0 0 0 0
3.75 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3.825 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3.85 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3.9 0000000 0 0 0 0 0 0 0 0 0 0
3.925 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3.975 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4.075 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4.125 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4.15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4.2 0000000 0 0 0 0 0 0 0 0 0 0
4.25 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4.325 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4.35 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4.425 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4.45 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4.475 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4.5 0000000 0 0 0 0 0 0 0 0 0 0
>
>
>
>
> table(LungCapData$LungCap)
0.507 1.025 1.125 1.175 1.325 1.45 1.575 1.625 1.675 1.775 1.85 1.9
1 1 1 1 1 1 1 1 1 1 1 1
1.925 1.95 2 2.025 2.25 2.375 2.475 2.55 2.625 2.65 2.725 2.825
1 1 1 1 3 1 1 1 2 2 3 1
2.85 2.875 2.925 2.95 3.025 3.1 3.175 3.225 3.25 3.4 3.425 3.45
1 3 2 1 1 2 2 1 1 1 3 1
3.6 3.625 3.65 3.675 3.7 3.75 3.825 3.85 3.9 3.925 3.975 4.075
1 1 1 2 2 1 1 1 3 2 2 1
4.125 4.15 4.2 4.25 4.325 4.35 4.425 4.45 4.475 4.5 4.525 4.55
1 1 2 3 3 1 3 2 2 1 1 1
4.575 4.625 4.65 4.7 4.725 4.775 4.8 4.825 4.85 4.875 4.9 4.95
1 3 1 2 3 1 1 1 2 1 2 1
4.975 5.025 5.05 5.075 5.125 5.15 5.175 5.2 5.225 5.25 5.275 5.3
3 3 3 1 1 3 1 1 2 2 1 1
5.325 5.35 5.375 5.425 5.475 5.5 5.55 5.575 5.6 5.625 5.65 5.675
2 1 3 1 1 2 3 1 1 1 3 1
5.7 5.725 5.775 5.825 5.85 5.875 5.95 6 6.05 6.075 6.1 6.125
1 2 1 1 4 4 3 2 4 3 3 3
6.15 6.175 6.2 6.225 6.25 6.275 6.3 6.325 6.375 6.4 6.425 6.45
3 3 3 4 1 1 2 2 1 1 1 7
6.475 6.5 6.525 6.55 6.575 6.6 6.625 6.65 6.675 6.7 6.725 6.75
3 2 2 3 4 1 2 2 1 5 4 2
6.775 6.8 6.825 6.85 6.9 6.925 6.95 6.975 7 7.025 7.05 7.075
1 2 3 4 3 2 4 2 2 2 2 1
7.1 7.125 7.15 7.175 7.2 7.225 7.25 7.275 7.3 7.325 7.35 7.375
2 1 1 1 1 2 2 4 2 4 5 3
7.4 7.425 7.45 7.475 7.5 7.55 7.575 7.625 7.65 7.675 7.7 7.725
4 2 3 4 1 6 2 3 3 2 3 1
7.75 7.775 7.8 7.825 7.85 7.875 7.9 7.925 7.95 7.975 8 8.025
2 1 2 7 3 2 3 5 3 2 7 4
8.05 8.075 8.1 8.125 8.175 8.2 8.225 8.25 8.275 8.3 8.325 8.35
1 2 1 4 1 3 4 3 3 1 2 8
8.375 8.425 8.45 8.475 8.5 8.525 8.55 8.575 8.6 8.625 8.65 8.675
1 5 1 2 5 1 3 2 6 6 2 1
8.7 8.725 8.775 8.8 8.825 8.85 8.875 8.9 8.925 8.975 9 9.025
3 4 7 3 3 3 2 2 1 5 3 4
9.05 9.1 9.125 9.15 9.175 9.2 9.225 9.275 9.3 9.325 9.35 9.375
3 5 1 2 3 3 2 3 1 3 1 3
9.4 9.45 9.475 9.5 9.525 9.55 9.575 9.6 9.625 9.65 9.675 9.7
1 2 4 1 3 3 1 1 3 1 5 2
9.725 9.75 9.8 9.825 9.85 9.875 9.9 9.925 9.95 9.975 10 10.025
3 3 2 4 3 2 2 3 2 1 1 4
10.05 10.1 10.125 10.175 10.2 10.25 10.275 10.3 10.325 10.35 10.375 10.4
3 3 2 2 5 1 2 2 1 2 1 4
10.425 10.45 10.475 10.5 10.525 10.55 10.575 10.6 10.625 10.65 10.675 10.7
2 3 4 2 1 1 1 4 1 2 2 4
10.725 10.75 10.775 10.8 10.825 10.85 10.875 10.9 10.925 10.95 10.975 11
3 1 2 1 2 1 2 2 3 1 2 1
11.025 11.05 11.075 11.125 11.175 11.225 11.275 11.3 11.325 11.35 11.4 11.5
2 1 4 2 1 5 1 1 2 1 2 3
11.525 11.55 11.575 11.6 11.625 11.65 11.675 11.7 11.75 11.775 11.8 11.825
1 1 2 1 1 2 1 2 1 1 2 1
11.875 11.9 11.95 12.05 12.125 12.15 12.2 12.225 12.275 12.325 12.375 12.4
2 2 1 2 2 2 1 2 1 3 1 1
12.425 12.5 12.625 12.7 12.9 12.925 12.95 13.025 13.05 13.075 13.1 13.2
2 1 1 1 1 1 2 1 1 1 1 1
1 3 1 1 1 1
> table(LungCapData$Age)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
13 6 20 25 37 41 40 51 58 68 69 56 64 54 43 43 37
> table(LungCapData$Height)
45.3 46.6 47 47.4 47.7 47.8 48 48.1 48.2 48.7 48.8 48.9 49 49.2 49.3 49.8
1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1
49.9 50.3 50.5 50.7 51 51.1 51.2 51.4 51.5 51.6 51.7 51.9 52 52.1 52.7 52.8
1 1 1 1 3 1 1 1 2 1 3 3 2 1 3 3
52.9 53 53.2 53.3 53.5 53.6 53.7 53.8 53.9 54.2 54.3 54.5 54.7 54.8 54.9 55
2 1 2 1 1 1 3 2 2 1 1 1 1 1 3 1
55.1 55.2 55.4 55.5 55.6 55.7 55.8 55.9 56 56.1 56.2 56.3 56.4 56.5 56.6 56.7
4 1 2 4 6 2 1 2 2 3 1 2 1 2 6 2
56.8 56.9 57 57.1 57.2 57.3 57.4 57.5 57.7 57.8 57.9 58 58.3 58.4 58.5 58.6
4 2 1 2 1 3 3 1 1 1 1 3 2 5 5 1
58.7 58.8 58.9 59 59.1 59.2 59.3 59.4 59.5 59.7 59.8 59.9 60 60.1 60.2 60.3
4 3 3 2 1 6 4 4 1 4 2 4 4 3 4 2
60.4 60.5 60.6 60.7 60.8 60.9 61 61.1 61.2 61.3 61.4 61.5 61.6 61.7 61.8 61.9
5 2 3 3 1 1 2 5 3 2 6 4 4 2 4 4
62 62.1 62.2 62.3 62.4 62.5 62.6 62.7 62.8 62.9 63 63.1 63.2 63.3 63.4 63.5
4 6 1 1 3 2 6 2 4 2 4 1 3 7 6 5
63.6 63.7 63.9 64 64.1 64.2 64.3 64.4 64.5 64.6 64.7 64.8 64.9 65 65.1 65.2
4 3 4 3 5 1 2 4 2 2 6 3 3 4 4 1
65.3 65.4 65.5 65.6 65.7 65.8 65.9 66 66.1 66.2 66.3 66.4 66.5 66.6 66.7 66.8
5 8 7 6 4 4 3 6 3 3 5 5 4 4 1 2
66.9 67 67.1 67.2 67.3 67.4 67.5 67.6 67.7 67.8 67.9 68 68.1 68.2 68.3 68.4
4 1 2 3 4 4 7 4 5 3 2 4 4 5 3 4
68.5 68.6 68.7 68.8 68.9 69 69.1 69.2 69.3 69.4 69.6 69.7 69.8 69.9 70 70.1
1 6 3 4 3 4 3 3 7 6 2 4 2 3 2 2
70.2 70.3 70.4 70.5 70.6 70.8 70.9 71 71.1 71.2 71.3 71.4 71.5 71.6 71.7 71.8
3 1 4 4 2 2 5 4 4 5 2 4 4 3 2 3
71.9 72 72.1 72.2 72.3 72.4 72.5 72.6 72.7 72.8 72.9 73 73.1 73.2 73.3 73.4
5 3 1 3 1 4 5 2 1 2 2 2 3 1 3 2
73.5 73.6 73.7 73.8 73.9 74 74.1 74.2 74.3 74.4 74.5 74.6 74.7 74.8 74.9 75
7 4 2 2 3 4 1 4 2 2 1 2 2 1 3 1
75.1 75.2 75.3 75.4 75.5 75.6 75.7 75.8 75.9 76 76.1 76.2 76.3 76.4 76.5 76.6
1 2 1 2 3 2 4 3 1 1 1 2 2 1 1 2
76.8 76.9 77.2 77.4 77.6 77.7 78 78.2 78.4 78.6 78.9 79.1 79.3 79.6 79.8 80.3
2 2 1 1 1 1 1 1 2 1 2 1 1 2 1 1
80.8 81.8
1 1
> table(LungCapData$Smoke)
no yes
648 77
> table(LungCapData$Gender)
female male
358 367
> table(LungCapData$Caesarean)
no yes
561 164
>