Question 1: Simple Table

assignment_data <- data.frame(
  Country = c("France", "Spain", "Germany", "Spain", "Germany",
              "France", "Spain", "France", "Germany", "France"),
  age = c(44, 27, 30, 38, 40, 35, 52, 48, 45, 37),
  salary = c(6000, 5000, 7000, 4000, 8000,
             5500, 4500, 6000, 7500, 5000),
  Purchased = c("No", "Yes", "No", "No", "Yes",
                "Yes", "No", "Yes", "No", "Yes")
)
knitr::kable(assignment_data)
Country age salary Purchased
France 44 6000 No
Spain 27 5000 Yes
Germany 30 7000 No
Spain 38 4000 No
Germany 40 8000 Yes
France 35 5500 Yes
Spain 52 4500 No
France 48 6000 Yes
Germany 45 7500 No
France 37 5000 Yes

The table contains 10 observations and four columns: Country, age, salary, and Purchased.

Question 2: Contingency Table

data(mtcars)
assignment9 <- table(mtcars$gear, mtcars$cyl,
                     dnn = c("gears", "cylinders"))
assignment9
##      cylinders
## gears  4  6  8
##     3  1  2 12
##     4  8  4  0
##     5  2  1  2

The table shows the number of cars for each combination of gears and cylinders. For example, 12 cars have 3 gears and 8 cylinders.

Question 2.1: Row and Column Totals

addmargins(assignment9)
##      cylinders
## gears  4  6  8 Sum
##   3    1  2 12  15
##   4    8  4  0  12
##   5    2  1  2   5
##   Sum 11  7 14  32

There are 32 cars in total. The row totals show that 15 cars have 3 gears, 12 have 4 gears, and 5 have 5 gears. The column totals show that 11 cars have 4 cylinders, 7 have 6 cylinders, and 14 have 8 cylinders.

Question 2.2: Overall Proportions

prop.table(assignment9)
##      cylinders
## gears       4       6       8
##     3 0.03125 0.06250 0.37500
##     4 0.25000 0.12500 0.00000
##     5 0.06250 0.03125 0.06250

Each cell is divided by the total of 32 cars. These proportions sum to 1 across the entire table. For example, cars with 3 gears and 8 cylinders account for 12/32 = 0.375, or 37.5% of all cars.

Question 2.3: Row Proportions

prop.table(assignment9, margin = 1)
##      cylinders
## gears          4          6          8
##     3 0.06666667 0.13333333 0.80000000
##     4 0.66666667 0.33333333 0.00000000
##     5 0.40000000 0.20000000 0.40000000

Each cell is divided by its row total, so each row sums to 1.

Among cars with 3 gears, approximately 6.67% have 4 cylinders, 13.33% have 6 cylinders, and 80% have 8 cylinders.

Among cars with 4 gears, approximately 66.67% have 4 cylinders, 33.33% have 6 cylinders, and 0% have 8 cylinders.

Among cars with 5 gears, 40% have 4 cylinders, 0% have 6 cylinders, and 60% have 8 cylinders.