Build a function in R that computes the probability of : a vector of length 4 that represents the number of balls of each of 4 colors (red, blue, green, yellow), and a vector of length 4 that represents the number of each ball chosen.
prob=function (x)
{ if(length(x)==4){
x/sum(x)
}
}
X=c(10,12,14,13)
prob(X)
[1] 0.2040816 0.2448980 0.2857143 0.2653061
prob=function (x,col=NULL)
{ if(length(x)==4){
if(class(col)=="NULL") col=1:4
x[col]/sum(x)
}
}
X=c(10,12,14,13)
# Here color are coded with 1,2,3,4
#red=1, blue=2, green=3, yellow=4 and number of balls are 10,12,14,13
prob(X,col=1)#for first color
[1] 0.2040816
prob(X,col=2)#for second color
[1] 0.244898
prob(X,col=3:4)# for third and fourth color
[1] 0.2857143 0.2653061
prob(X) # for probability of a first to fourth color
[1] 0.2040816 0.2448980 0.2857143 0.2653061
Get Answers For Free
Most questions answered within 1 hours.