Tables are suitable for various purposes in R and provide a convenient way to work with structured data. I use tables here to view and explore my data in the following exercises.
Here is how I have started. Note: I have already installed the package “data.table”
library(“data.table”)
## Warning: package ‘data.table’ was built under R version 4.2.3
#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”))
### 1. Generate simple table in R that consists of four rows: Country, age, salary and purchased
setDT(assignment_data)
my_table <- assignment_data[, .(Country, age, salary, Purchased)]
# Print the table
print(my_table)
## 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
######################### end of exercise 1 ##############################/
### 2. Generate contingency table also know as r x C table using mtcars dataset.
# In this step, I am using the `table()` function to create a contingency table named `assignment9`.
# The table shows how different gear options (“gears”: 3, 4, and 5) are distributed across different cylinder options (“cyl”: 4, 6, and 8) in the `mtcars` dataset.
# The printed table displays the counts for each combination of gear and cylinder categories.
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
# I am using the `addmargins()` function to add sum totals to the contingency table.
# This helps me to see the total counts for each row and column.
addmargins(assignment9)
## 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.
# In this step, I apply the `prop.table()` function to the `assignment9` table.
# It calculates the proportional weight of each count in the table relative to the total count.
# The prop.table accepts a table, and a margin= argument, With no value specified for margin=,
# the sum of all the cells in the table will be 1.
prop.table(assignment9)
## 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.
# Here, I refine the analysis by specifying `margin = 1` in the `prop.table()` function.
# With margin=1, each row of the resulting table will add to 1.
prop.table(assignment9, margin = 1)
## 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
# In summary, using R to generate a contingency table, add sum totals, calculate proportional weights, and explore row proportions enabled me
# to gain insights into the distribution of gear and cylinder combinations in the `mtcars` dataset.
# Contingency tables and these additional analyses are valuable tools for understanding relationships between categorical variables in data analysis.
######################### end of exercise 2 ###########################/