Skip to main content

Module #9 Assignment

 This week the code was quite easy to implement which made understanding the underlying principle a little easier I feel like my understanding of how to navigate R and it's processes is getting better along with my understanding of more complex statistic knowledge.

 

 

# 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.
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 r x c table (Chapter 7, p.135) using mtcars dataset.
assignment9 <- table(mtcars$gear, mtcars$cyl, dnn=c("gear","cyl"))
assignment9
##     cyl
## gear  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)
##      cyl
## gear   4  6  8 Sum
##   3    1  2 12  15
##   4    8  4  0  12
##   5    2  1  2   5
##   Sum 11  7 14  32
assignment9Margin<-addmargins(assignment9)
                     
# 2.2 Add prop.tables() function, and report on the proportional weight of each value in a assignment9 table
prop.table(assignment9)
##     cyl
## gear       4       6       8
##    3 0.03125 0.06250 0.37500
##    4 0.25000 0.12500 0.00000
##    5 0.06250 0.03125 0.06250
prop.table(assignment9Margin)
##      cyl
## gear          4         6         8       Sum
##   3   0.0078125 0.0156250 0.0937500 0.1171875
##   4   0.0625000 0.0312500 0.0000000 0.0937500
##   5   0.0156250 0.0078125 0.0156250 0.0390625
##   Sum 0.0859375 0.0546875 0.1093750 0.2500000
# 2.3 Add margin = 1 to the argument under prop.table() function, and report on the row proportions found in assignment9 table.
prop.table(assignment9,margin = 1)
##     cyl
## gear          4          6          8
##    3 0.06666667 0.13333333 0.80000000
##    4 0.66666667 0.33333333 0.00000000
##    5 0.40000000 0.20000000 0.40000000
prop.table(assignment9Margin,margin = 1)
##      cyl
## gear           4          6          8        Sum
##   3   0.03333333 0.06666667 0.40000000 0.50000000
##   4   0.33333333 0.16666667 0.00000000 0.50000000
##   5   0.20000000 0.10000000 0.20000000 0.50000000
##   Sum 0.17187500 0.10937500 0.21875000 0.50000000

Comments