Merge
Merge
2(Solution)
in_hand_salary = function(Monthly_Salary,leaves){
if(A != "yes"){
print("you quitted the game")
break
}
}
else if(Guess == a ){
print("Congratulations your guess is correct")
print(paste("The number was -", a ,"Your guess was - ", Guess))
break
}
}
View(products)
Products_purchased = c("Juice", "Sugar", "Sugar", "Mixer", "Biscuits", "Biscuits", "Biscuits",
"Biscuits", "Chocolates", "Chocolates", "Chocolates", "Umbrella", "Toaster", "School bag", "School
bag","Umbrella", "Spices", "Dry fruits", "Sugar", "Salt", "Chocolates", "Bottles", "Ketchup",
"Ketchup", "Trimmer", "Detergent", "Detergent")
library(dplyr)
df<- productName %>% group_by(Products_purchased) %>% summarise(total_cnt = n())
df<- merge(df, productPrice,by.x = "Products_purchased" , by.y - "Products", all - TRUE)
View(df)
df$total_price <- df$total_cnt * df$productPrice
df[is.na(df)] <- "-"
View(df)
Practical 3: Graphs and Diagrams
Q1.
(a)
> country=c("India","Germany","China","Pakistan","Sweden")
> birth_rate=c(33,16,40,35,15)
> country
[1] "India" "Germany" "China" "Pakistan" "Sweden"
> birth_rate
[1] 33 16 40 35 15
> #a - bar chart
> barplot(birth_rate,names.arg=country,col="blue",main="Birth Rate per 1000 for different
countries",xlab="Country",ylab="Birth Rate")
(b)
> country=c("India","Germany","China","Pakistan","Sweden")
> birth_rate=c(33,16,40,35,15)
> #b - pie chart
> pie(birth_rate,main="Birth rate per 1000 for different countries - Pie
Chart",labels=country,radius=1,col=rainbow (length(birth_rate)))
Q2.
> max=c(40.5,42.8,37.8,39.4)
> min=c(34.7,33.5,32.2,33.1)
> d=data.frame(max,min,row.names=c("Delhi","Kolkata","Mumbai","Chennai"))
>d
max min
Delhi 40.5 34.7
Kolkata 42.8 33.5
Mumbai 37.8 32.2
Chennai 39.4 33.1
> boxplot(d,main="Max and Min Temperatures",col=c("lightblue","pink"),names=c("Max","Min"))
Q3.
> years=c("2000-2001","2001-2002","2002-2003")
> y1=c(20,10,5)
> y2=c(25,9,10)
> y3=c(30,20,20)
> d=data.frame(y1,y2,y3)
>d
y1 y2 y3
1 20 25 30
2 10 9 20
3 5 10 20
> d1=as.matrix(d)
> d1
y1 y2 y3
[1,] 20 25 30
[2,] 10 9 20
[3,] 5 10 20
> barplot(d1,beside=F,names.arg=years,col=1:2:3,legend=c("2000-2001","2001-
2002","2002- 2003"),xlab="Years",ylab="No. of students")
Warning message:
In 1:2:3 : numerical expression has 2 elements: only the first used
Q4.
> midx=seq(30,70,10)
> f=c(6,12,25,16,11)
> cls_limit=seq(25,75,10)
> y=rep(midx,f)
>y
[1] 30 30 30 30 30 30 40 40 40 40 40 40 40 40 40 40 40 40 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 60 60 60 60 60 60 60
[51] 60 60 60 60 60 60 60 60 60 70 70 70 70 70 70 70 70 70 70 70
> hist(y,breaks=cls_limit,col="red",main="Histogram of data",xlab="Marks",ylab="No. of students")
Q5.
> lb=seq(0,40,10)
> ub=seq(10,50,10)
> midx=(lb+ub)/2
> f=c(6,12,25,16,11)
> x0=c(0,midx,50)
> y0=c(0,f,0)
> y=rep(midx,f)
>y
[1] 5 5 5 5 5 5 15 15 15 15 15 15 15 15 15 15 15 15 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25
25 25 25 25 25 25 25 25 35 35 35 35 35 35 35
[51] 35 35 35 35 35 35 35 35 35 45 45 45 45 45 45 45 45 45 45 45
> bks=seq(0,50,10)
> hist(y,breaks=bks,main="Frequency curve and polygon",col="purple",xlab="Marks",ylab="No. of
students")
> lines(x0,y0)
Q6.
> f=c(12,24,43,38,22,11)
> lc=cumsum(f)
> lc
[1] 12 36 79 117 139 150
> uc=1:6
> uc
[1] 1 2 3 4 5 6
> for (i in 6:1){
+ uc[i]=sum(f[6:i])}
> uc
[1] 150 138 114 71 33 11
> lbx=seq(19.5,45.5,5)
> lbx
[1] 19.5 24.5 29.5 34.5 39.5 44.5
> ubx=seq(24.5,49.5,5)
> ubx
[1] 24.5 29.5 34.5 39.5 44.5 49.5
> plot(ubx,lc,type="l",xlim=c(15,50),xlab="Age",ylab="Cumulative frequency",lwd=2)
> lines(lbx,uc,type="l",xlim=c(15,50),xlab="Age",ylab="Cumulative frequency",lwd=2)
Q7.
> x=c(50,50,55,60,65,65,65,60,60,50)
> y=c(11,13,14,16,16,15,15,14,13,13)
> plot(x,y,main="Scatterplot - Sales vs Expenses",xlab="Sales",ylab="Expenses")
Q8.
> marks=c(23,42,12,10,15,14,23,45,10,11,34,56,23,34,19)
> boxplot(marks,main="Marks",xlab="JD College",ylab="Marks of students",col="orange")
Q9.
> jan=c(23,34,32,31,34,35,23,27,34)
> feb=c(23,21,35,34,36,25,26,23,21)
> mar=c(33,35,26,27,28,21,25,24,23)
> apr=c(22,21,23,34,35,39,38,37,36)
> may=c(38,37,39,40,35,37,41,38,39)
> d=data.frame(jan,feb,mar,apr,may)
>d
jan feb mar apr may
1 23 23 33 22 38
2 34 21 35 21 37
3 32 35 26 23 39
4 31 34 27 34 40
5 34 36 28 35 35
6 35 25 21 39 37
7 23 26 25 38 41
8 27 23 24 37 38
9 34 21 23 36 39
> boxplot(d,main="Temperature",xlab="Month",ylab="Degrees in
Celcius",names=c("January","February","March","April","May"),col=topo.colors(5))
Q10.
> x1=c(60,68,50,66,60,55,72,60,62,51)
> x2=c(42,56,45,64,50,55,57,48,56,42)
> x3=c(74,71,78,80,72,62,70,70,76,65)
> df=data.frame(x1,x2,x3)
> corrplot(cor(df),method="square",col=c("pink","grey"))
Practical 4: Probability Distributions
Q1.
> a = dbinom(0,n,p)
>a
[1] 0.0001048576
> b = dbinom(2,n,p)
>b
[1] 0.01061683
> c = pbinom(3,n,p)
>c
[1] 0.05476188
> d = 1 - pbinom(5,n,p)
>d
[1] 0.6331033
Q2.
> m=3.2
> a = dpois(3,m)
>a
[1] 0.222616
> b = dpois(5,m)
>b
[1] 0.1139794
> c = ppois(1,m)
>c
[1] 0.1712013
> d = 1 - ppois(3,m)
>d
[1] 0.3974803
> e = 1 - ppois(4,m)
>e
[1] 0.2193875
Q3.
> mean=10
> a = 1 - pexp(1,mean)
>a
[1] 4.539993e-05
> b = pexp(6,mean)
>b
[1] 1
> c = pexp(5,mean)
>c
[1] 1
> d = 1 - pexp(6,mean)
>d
[1] 0
Q4.
> mu=50
> sigma=sqrt(40)
> a = pnorm(60,mu,sigma)
>a
[1] 0.9430769
> b = 1 - pnorm(99,mu,sigma)
>b
[1] 4.662937e-15
>c
[1] 1.050591e-06
> d = qnorm(0.293,mu,sigma)
>d
[1] 46.55538
Q5.
(a)
PMF:
> # X ~ P(2.6)
> m=2.6
> x=0:50
> prob=dpois(x,m)
> head(pois_pmf)
x Prob
1 0 0.0743
2 1 0.1931
3 2 0.2510
4 3 0.2176
5 4 0.1414
6 5 0.0735
> plot(x,prob,"h")
CDF:
> # X ~ P(2.6)
> m=2.6
> x=0:50
> prob=ppois(x,m)
> head(pois_cdf)
x Prob
1 0 0.0743
2 1 0.2674
3 2 0.5184
4 3 0.7360
5 4 0.8774
6 5 0.9510
> plot(x,prob,"s")
(b)
PMF:
> # X ~ Bin(8,0.65)
> x=0:n
> prob=dbinom(x,n,p)
> binom_pmf=data.frame(x,Prob=round(prob,4))
> head(binom_pmf)
x Prob
1 0 0.0002
2 1 0.0033
3 2 0.0217
4 3 0.0808
5 4 0.1875
6 5 0.2786
> plot(x,prob,"h")
CDF:
> plot(x,prob,"h")
> # X ~ Bin(8,0.65)
> x=0:n
> prob=pbinom(x,n,p)
> binom_cdf=data.frame(x,Prob=round(prob,4))
> head(binom_cdf)
x Prob
1 0 0.0002
2 1 0.0036
3 2 0.0253
4 3 0.1061
5 4 0.2936
6 5 0.5722
> plot(x,prob,"s")
Q6.
(a)
PDF:
> # X ~ Exp(5)
> m=5
> x=0:20
> prob=dexp(x,m)
> exp_pdf=data.frame(x,Prob=round(prob,4))
> head(exp_pdf)
x Prob
1 0 5.0000
2 1 0.0337
3 2 0.0002
4 3 0.0000
5 4 0.0000
6 5 0.0000
> plot(x,prob)
CDF:
> # X ~ Exp(5)
> m=5
> x=0:20
> prob=pexp(x,m)
> exp_cdf=data.frame(x,Prob=round(prob,4))
> head(exp_cdf)
x Prob
1 0 0.0000
2 1 0.9933
3 2 1.0000
4 3 1.0000
5 4 1.0000
6 5 1.0000
> plot(x,prob)
(b)
PDF:
> # X ~ N(10,16)
> x=seq(-2,2,by=0.02)
> prob=dnorm(x,mu,sigma)
> norm_pdf=data.frame(x,Prob=round(prob,4))
> head(norm_pdf)
x Prob
1 -2.00 0.0011
2 -1.98 0.0011
3 -1.96 0.0011
4 -1.94 0.0012
5 -1.92 0.0012
6 -1.90 0.0012
> plot(x,prob)
CDF:
> # X ~ N(10,16)
> x=seq(-2,2,by=0.02)
> prob=pnorm(x,mu,sigma)
> norm_cdf=data.frame(x,Prob=round(prob,4))
> head(norm_cdf)
x Prob
1 -2.00 0.0013
2 -1.98 0.0014
3 -1.96 0.0014
4 -1.94 0.0014
5 -1.92 0.0014
6 -1.90 0.0015
> plot(x,prob)
Practical 5: Analysis of Variance (One way and Two way)
Q1.
To test, H0: no significant difference between scores of all three groups against H1: not H0
>values=c(85,86,88,75,78,94,98,79,71,80,91,92,93,85,87,84,82,88,95,96,79,78,88,94,92,85,83,85,82,81)
>groups=c(rep("Group1",10),rep("Group2",10),rep("Group3",10))
>df=data.frame(groups,values)
>a=aov(values~groups,data=df)
Conclusion: since p value (0.114) is not less than α = 0.05, we do not reject H0.
_______________________________________________________________________________________
Q2.
To test, H0: no significant difference between the mean heights of men above 20 years of
age across three different countries such as US, UK, and India against H1: not H0
>heights=c(180,183,172,178,169,179,178,180,185,181,180,179,164,173,180,178,170,183,180,175,181, 183,
176,167)
>df=data.frame(countries,heights)
>a=aov(heights~countries,data=df)
Conclusion: since p value (0.974) is not less than α = 0.05, we do not reject H0.
_______________________________________________________________________________________
Q3.
To test, H0: Acidity of rain does not have a significant effect on Alaska, Florida and
Texas against H1: not H0
>Acidity=c(5.11,5.01,4.90,5.14,4.80,5.24,4.87,4.18,4.40,4.67,4.89,4.09,5.46,6.29,5.57,5.15,5.45,5.30)
>Site=c(rep("Alaska",6),rep("Florida",6),rep("Texas",6))
>Df=data.frame(Site,Acidity)
>A=aov(Acidity~Site,data=Df)
Conclusion: since p value (0.000229) is less than α = 0.05, we reject H0.
_______________________________________________________________________________________
Q4.
To test, H0: dosage level does not have a significant effect on cholesterol level against
H1: not H0
>dosage=c(210,240,270,270,300,210,240,240,270,270,180,210,210,210,240)
>df=data.frame(groups,dosage)
>a=aov(dosage~groups,data=df)
Conclusion: Since p value (0.0424) is less than α = 0.05, we reject H0 and accept H1: dosage level
does not have a significant effect on cholesterol level.
__________________________________________________________________________________________
Q5.
To test, H01: There is no significant difference between all methods against H11: not H01
H02: There is no significant difference between all Analyst against H12: not H02
>sample=c(7.5,7.4,7.3,7.6,7.4,7.0,7.2,7.0,7.2,7.1,7.1,6.7,6.9,6.8,6.9)
>method=c(rep("M1",5),rep("M2",5),rep("M3",5))
>analyst=c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
>a=aov(sample~method+analyst)
Conclusion: Since p-value (9.09e-05) are less than α = 0.05, we reject H01 .
And p-value (0.889) are not less than α = 0.05, we do not reject H02 .
_______________________________________________________________________________________
Q6.
To test, H01: There is no significant difference between all determination against H11: not H01
H02: There is no significant difference between all Analyst against H12: not H02
>experiment=c(1.963,2.958,2.956,2.948,2.953,2.941,2.996,2.964,2.945,2.960,2.961,2.940,2.675,2.955,
2.963,2.953,2.261,2.931,2.970,2.241,2.975,2.950,2.949,2.930,2.979,2.950,2.958,2.951,2.955,2.937)
>determination=c(rep("1",6),rep("2",6),rep("3",6),rep("4",6),rep("5",6))
>analyst=c(1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6)
>a=aov(experiment~determination+analyst)
Conclusion: Since p-values (0.603, 0.261) are not less than α = 0.05, we do not reject H01 and H02.
>transportation=data.frame(D1=c(5,4,3,16),D2=c(3,7,4,18),D3=c(6,9,7,31),D4=c(2,1,5,25),Supply=c(19,37
,34,90),row.names=c("O1","O2","O3","Demand"))
> transportation
D1 D2 D3 D4 Supply
O1 5 3 6 2 19
O2 4 7 9 1 37
O3 3 4 7 5 34
Demand 16 18 31 25 90
> library(TransP)
> nwc(transportation)
$Alloc_Matrix
D1 D2 D3 D4
O1 16 3 0 0
O2 0 15 22 0
O3 0 0 9 25
$Total_Cost
[1] 580
b)
> colnames=c("D1","D2","D3","D4","Supply")
> rownames=c("O1","O2","O3","Demand")
>
transp=matrix(c(5,4,3,16,3,7,4,18,6,9,7,31,2,1,5,25,19,37,34,90),nrow=4,ncol=5,dimnames=list(rowname
s,colnames))
> transp
D1 D2 D3 D4 Supply
O1 5 3 6 2 19
O2 4 7 9 1 37
O3 3 4 7 5 34
Demand 16 18 31 25 90
> mincost(transp)
$Alloc_Matrix
D1 D2 D3 D4
O1 16 3 0 0
O2 0 0 12 25
O3 0 15 19 0
$Total_Cost
[1] 415
1)
2)
>
data=data.frame(Destination1=c(25,30,15,200),Destination2=c(45,65,40,100),Destination3=c(10,15,
55,300),Destination4=c(0,0,0,100),Supply=c(200,100,400,700),row.names=c("SOURCE1","SOURCE2",
"SOURCE3","DEMAND"))
> data
SOURCE2 30 65 15 0 100
SOURCE3 15 40 55 0 400
> library(TransP)
> a=nwc(data)
>a
$Alloc_Matrix
SOURCE1 200 0 0 0
SOURCE2 0 100 0 0
$Total_Cost
[1] 28000
LCM
> rownames=c("SOURCE1","SOURCE2","SOURCE3","DEMAND")
> colnames=c("DESTINATION1","DESTINATION2","DESTINATION3","DESTINATION4","SUPPLY")
>data=matrix(c(25,35,10,0,200,30,65,15,0,100,15,40,55,0,400,200,100,300,100,700),nrow=4,byrow=TRU
E,dimnames=list(rownames,colnames))
> data
SOURCE3 15 40 55 0 400
> a=mincost(data)
>a
$Alloc_Matrix
SOURCE2 100 0 0 0
$Total_Cost
[1] 20500
2) Max Z= 3X1+2X2+5X3
3X1+2X2+6X3≤12
2X1+3X2+4X3≤12
> objective_function=c(3,2,5)
> constrains=matrix(c(1,2,2,3,2,6,2,3,4),nrow=3,byrow=TRUE)
> constrain_sign=c("<=","<=","<=")
> rhs=c(8,12,12)
> library(lpSolve)
[1] 4 0 0
>a
3)
Equations
Max=25x+20y
Constrains are:
20x+12y<=2000
5x+5y<=540
x,y>=0
> objective_function=c(25,20)
> constrains=matrix(c(20,12,5,5),nrow=2,byrow=TRUE)
> constrain_sign=c("<=","<=")
> rhs=c(2000,540)
[1] 88 20
>a
4)
> rownames=c("A","B","C","D")
> colnames=c("M1","M2","M3","M4")
>
a=matrix(c(10,12,9,11,5,10,7,8,12,14,13,11,8,15,11,9),byrow=TRUE,nrow=4,ncol=4,dimnames=list(rowna
mes,colnames))
> s=lp.assign(a)
>s
[1,] 0 0 1 0
[2,] 1 0 0 0
[3,] 0 1 0 0