Module 9 Assignment

This weeks assignment is based around two questions, the first is to generate a simple table in R that consists of four rows: Country, age, salary, and purchased. The following code is used to generate that.

# 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), 
                               Purchased=c("No","Yes","No","No","Yes", "Yes","No","Yes","No","Yes"))
print(assignment_data)

The second is used to generate a contingency table known as a rx C table using the mtcars dataset. In addition to this, there must be a report on the sum of the rows and columns, the proportional weight of each value in the table, and the row proportions of the table. This can be seen in the following code.

# 2. Contingency table
library(datasets)
data(mtcars)
assignment9 <- table(mtcars$gear, mtcars$cyl, dnn=c("gears", "cylinders"))
print(assignment9)

# 2.1 Add margins
assignment9_margins <- addmargins(assignment9)
print(assignment9_margins)

# 2.2 Proportional table
assignment9_prop <- prop.table(assignment9)
print(assignment9_prop)

# 2.3 Row proportions
assignment9_row_prop <- prop.table(assignment9, margin = 1)
print(assignment9_row_prop)
The complete code with outputs would be:

Comments