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), Purchased=c("No","Yes","No","No","Yes", "Yes","No","Yes","No","Yes"))
Generate simple table in R that consists of four rows: Country, age, salary and purchased.
In order to make a table from the dataframe, I loaded the data.table package. This is the result:
> setDT(assignment_data) > assignment_data Country age salary Purchased 1: France 44 6000 No 2: Spain 27 5000 Yes 3: Germany 30 7000 No 4: Spain 38 4000 No 5: Germany 40 8000 Yes 6: France 35 6000 Yes 7: Spain 52 5000 No 8: France 48 7000 Yes 9: Germany 45 4000 No 10: France 37 8000 Yes
2. Generate contingency table also know as rx C table using mtcars dataset.
>assignment9 <- table(mtcars$gear, mtcars$cyl, dnn=c("gears"
assignment9 <- table(mtcars$gear, mtcars$cyl, dnn=c("gears", "cyl")) > print(assignment9) cyl gears 4 6 8 3 1 2 12 4 8 4 0 5 2 1 2
2.1 Add the addmargins() function to report on the sum totals of the rows and columns of assignment9 table
>addmargins(assignment9)
> tablea <- addmargins(assignment9) > tablea cyl 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
2.2 Add prop.tables() function, and report on the proportional weight of each value in a assignment9 table
tableb <- prop.table(assignment9) > tableb cyl 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
2.3 Add margin = 1 to the argument under prop.table() function, and report on the row proportions found in assignment9 table.
tablec <- prop.table(assignment9, margin = 1) > print(tablec) cyl 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
Comments
Post a Comment