Module #9 Assignment
Module #9 Assignment
Generating a simple table for the given data.frame assignment_data:
simple_table <- assignment_data[, c("Country", "age", "salary", "Purchased")]
Generating a contingency table using the mtcars dataset:
assignment9 <- table(mtcars$gear, mtcars$cyl, dnn = c("gears", "cylinders"))
2.1. Adding row and column totals to the assignment9 table using addmargins:
assignment9_with_totals <- addmargins(assignment9)
2.2. Calculating proportional weights of each value in the assignment9 table using prop.tables:
proportional_weights <- prop.table(assignment9)
2.3. Calculating row proportions in the assignment9 table using prop.table with margin = 1:
row_proportions <- prop.table(assignment9, margin = 1)
Code & 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")
)
simple_table <- assignment_data[, c("Country", "age", "salary", "Purchased")]
library(datasets)
data(mtcars)
assignment9 <- table(mtcars$gear, mtcars$cyl, dnn = c("gears", "cylinders"))
assignment9_with_totals <- addmargins(assignment9)
proportional_weights <- prop.table(assignment9)
row_proportions <- prop.table(assignment9, margin = 1)
print("Simple Table:")
print(simple_table)
print("Contingency Table with Totals:")
print(assignment9_with_totals)
print("Proportional Weights:")
print(proportional_weights)
print("Row Proportions:")
print(row_proportions)
Comments
Post a Comment