Module #9 Assignment
Module #9 Assignment
1. Your data.frame is
> 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"))
Generate simple table in R that consists of four columns: Country, age, salary and purchased.
When running this code, the output will be a data frame with the columns "Country", "age", "salary", and "Purchased".
2. Generate contingency table also known as r x c table using mtcars dataset i.e. data(mtcars)
assignment9 <- table(mtcars$gear, mtcars$cyl, dnn=c("gears", "cylinders")
This code will create a table showing the frequency counts of cars based on their number of gears(rows) and cylinders(columns) in the mtcars dataset. The dnn parameter labels the dimensions of the table as "gears" and "cylinders". We can see information such as 1 car with 3 gears and 4 cylinders and 12 cars with 3 gears and 8 cylinders.
2.1 Add the addmargins() function to report on the sum totals of the rows and columns of assignment9 table
>addmargins(assignment9)
The code will display the assignment9 table with an additional row and column showing the sum totals for each gear and cylinder category. The addmargins() function includes these margins, giving you a complete view of counts along with their totals. Now we have information such as a total of 15 cars with 3 gears and 11 cars with 4 cylinders.
2.2 Add prop.tables() function, and report on the proportional weight of each value in a assignment9 table
2.3 Add margin = 1 to the argument under prop.table() function, and report on the row proportions found in assignment9 table.
margin = 1 indicates that you want to compute the proportions for each row. The resulting table will show the proportion of each cylinder count within each gear category, helping you understand the distribution of cylinder counts relative to the total for each gear user. For example, a car with 3 gears with 4, 6, and 8 cylinders equal to 1.
0.06666667 + 0.13333333 + 0.80000000 = 1
A breakdown of the proportions can be provided:
1/15 = 0.06666667
2/15 = 0.13333333
12/15 = 0.8
In this assignment I learned how to create a data frame in R and generate a contingency table using the table() function. With this I was able to summarize the relationship between two variables, gear and cyl from the mtcars dataset. I also learned how to enhance the table's readability by adding row and column totals with the addmarings() function. I also used the prop.table() function to calculate the proportional weights of the values in the contingency table. Lastly, I learned how to compute row proportions using prop.table() with margin = 1, which allowed me to understand how the counts of cylinders are distributed within each gear category.
Comments
Post a Comment