rm(list=ls()) # remove all objects from environment cat(“) # Clear the console graphics.off() # Clear all graphs gc() # Clear unused memory
#Question 1
d1<-c('1','2','3','4','5','6')
d2<-c('1','2','3','4','5','6')
d3<-c('1','2','3','4','5','6')
df<-expand.grid(d1,d2,d3)
mdf<-as.matrix(df)
as.numeric(mdf)
## [1] 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 1 2 3 4 5 6 1
## [38] 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 1 2 3 4 5 6 1 2
## [75] 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 1 2 3 4 5 6 1 2 3
## [112] 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 1 2 3 4 5 6 1 2 3 4
## [149] 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 1 2 3 4 5 6 1 2 3 4 5
## [186] 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 1 2 3 4 5 6 1 1 1 1 1 1
## [223] 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 1 1 1 1 1 1 2
## [260] 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 1 1 1 1 1 1 2 2
## [297] 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 1 1 1 1 1 1 2 2 2
## [334] 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 1 1 1 1 1 1 2 2 2 2
## [371] 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 1 1 1 1 1 1 2 2 2 2 2
## [408] 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 1 1 1 1 1 1 1 1 1 1 1 1
## [445] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2
## [482] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## [519] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
## [556] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
## [593] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
## [630] 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
is.numeric(mdf)
## [1] FALSE
55/216-30/216
## [1] 0.1157407
#Question 2
A <- matrix( c(200, 300, 200, 100, 100, 200, 200, 100, 200, 100),
nrow = 5,
ncol = 2,
byrow = TRUE
)
rownames(A) = c("apartment", "Dorm", "With parents", "soroity/fraternity house", "other")
colnames(A) = c("males", "females")
cat("gender and reidence of customers:\n")
## gender and reidence of customers:
print(A)
## males females
## apartment 200 300
## Dorm 200 100
## With parents 100 200
## soroity/fraternity house 200 100
## other 200 100
rowSums(A, na.rm = FALSE, dims = 1L)
## apartment Dorm With parents
## 500 300 300
## soroity/fraternity house other
## 300 300
colSums(A, na.rm = FALSE, dims = 1L)
## males females
## 900 800
200/300
## [1] 0.6666667
#Question3
13/52*12/51
## [1] 0.05882353
#Question4
x<- c(‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’,‘10’,‘11’,‘12’,‘13’,‘14’,‘15’,‘16’,‘17’,‘18’,‘19’,‘20’)
permutations(n=20, r=10, v=x,)
nrow(permutations(n=20,r=10,v=x))
#question5
perm_without_replacement <- function(n, r){
return( factorial(n + r - 1) / (factorial(r) * factorial(n - 1)) )
}
perm_without_replacement(18,3)
## [1] 1140
#questioin6
perm_without_replacement <- function(n, r){
return(factorial(n)/factorial(n - r))
}
#10 choices, choose 10
perm_without_replacement(10,10)
## [1] 3628800
#Question7
2^7*6^3*(52*51*50*49)
## [1] 179640115200
#question8
#(2*4!*4!)/8=144
#question9
aGivenb <- 0.95
notaGivenNotb <- 0.99
aGivenNotb <- 0.01
b <- 0.03
notb <- 0.97
bgivena <- (aGivenb*b)/(aGivenb*b+aGivenNotb*notb)
print(bgivena)
## [1] 0.7460733
#question 10 ## the prob is 1/2. Because there are only two pancakes with brown side on it, and the total combination of having brown side on the pancake are b/b and b/g,so the prob is 0.5.