1. The data set UCBAdmissions is a 3-way table of frequencies classified by Admit, Gender, and Dept.

data(UCBAdmissions)
dimnames(UCBAdmissions)
## $Admit
## [1] "Admitted" "Rejected"
## 
## $Gender
## [1] "Male"   "Female"
## 
## $Dept
## [1] "A" "B" "C" "D" "E" "F"
  1. Find the total number of cases contained in this table
ftable(UCBAdmissions)
##                 Dept   A   B   C   D   E   F
## Admit    Gender                             
## Admitted Male        512 353 120 138  53  22
##          Female       89  17 202 131  94  24
## Rejected Male        313 207 205 279 138 351
##          Female       19   8 391 244 299 317
  1. For each department, find the total number of applicants
dtotal<-apply(UCBAdmissions, c(3), sum)
dtotal
##   A   B   C   D   E   F 
## 933 585 918 792 584 714
  1. For each department, find the overall proportion of applicants who were admitted.
femaleacc=UCBAdmissions[1,1,]
maleacc=UCBAdmissions[1,2,]
total=apply(UCBAdmissions, c(3), sum)
apct <- (femaleacc+maleacc)/(total)
apct
##          A          B          C          D          E          F 
## 0.64415863 0.63247863 0.35076253 0.33964646 0.25171233 0.06442577
  1. Construct a tabular display of department (rows) and gender (columns), showing the proportion of applicants in each cell who were admitted relative to the total applicants in that cell.
Admit=UCBAdmissions[1,,]
Reject=UCBAdmissions[2,,]
pct <- Admit/(Admit+Reject)
pct
##         Dept
## Gender            A          B          C          D          E          F
##   Male   0.62060606 0.63035714 0.36923077 0.33093525 0.27748691 0.05898123
##   Female 0.82407407 0.68000000 0.34064081 0.34933333 0.23918575 0.07038123

2. The data set UKSoccer in vcd gives the distributions of number of goals scored by the 20 teams in the 1995/96 season of the Premier League of the UK Football Association.

data("UKSoccer", package="vcd")
ftable(UKSoccer)
##      Away  0  1  2  3  4
## Home                    
## 0         27 29 10  8  2
## 1         59 53 14 12  4
## 2         28 32 14 12  4
## 3         19 14  7  4  1
## 4          7  8 10  2  0
dimnames(UKSoccer)
## $Home
## [1] "0" "1" "2" "3" "4"
## 
## $Away
## [1] "0" "1" "2" "3" "4"
  1. Verify that the total number of games represented in this table is 380
total=apply(UKSoccer, c(1), sum)
sum(total)
## [1] 380
  1. Find the marginal total of the number of goals scored by each of the home and away teams.
addmargins(UKSoccer,1)
##      Away
## Home    0   1   2   3   4
##   0    27  29  10   8   2
##   1    59  53  14  12   4
##   2    28  32  14  12   4
##   3    19  14   7   4   1
##   4     7   8  10   2   0
##   Sum 140 136  55  38  11
addmargins(UKSoccer,2)
##     Away
## Home   0   1   2   3   4 Sum
##    0  27  29  10   8   2  76
##    1  59  53  14  12   4 142
##    2  28  32  14  12   4  90
##    3  19  14   7   4   1  45
##    4   7   8  10   2   0  27

(c)Express each of the marginal totals as proportions

prop.table(UKSoccer,1)
##     Away
## Home          0          1          2          3          4
##    0 0.35526316 0.38157895 0.13157895 0.10526316 0.02631579
##    1 0.41549296 0.37323944 0.09859155 0.08450704 0.02816901
##    2 0.31111111 0.35555556 0.15555556 0.13333333 0.04444444
##    3 0.42222222 0.31111111 0.15555556 0.08888889 0.02222222
##    4 0.25925926 0.29629630 0.37037037 0.07407407 0.00000000
prop.table(UKSoccer,2)
##     Away
## Home          0          1          2          3          4
##    0 0.19285714 0.21323529 0.18181818 0.21052632 0.18181818
##    1 0.42142857 0.38970588 0.25454545 0.31578947 0.36363636
##    2 0.20000000 0.23529412 0.25454545 0.31578947 0.36363636
##    3 0.13571429 0.10294118 0.12727273 0.10526316 0.09090909
##    4 0.05000000 0.05882353 0.18181818 0.05263158 0.00000000