Data Exploration
Exercises ~ Week 3
1 Exercise 1
The following table shows sample information for three students. Each observation represents a single student and includes details such as their unique student ID, name, age, total credits completed, major field of study, and year level.
This dataset demonstrates a mixture of variable types:
- Nominal: StudentID, Name, Major
- Numeric: Age (continuous), CreditsCompleted
(discrete)
- Ordinal: YearLevel (Freshman → Senior)
StudentID | Name | Age | CreditsCompleted | Major | YearLevel |
---|---|---|---|---|---|
S001 | Alice | 20 | 45 | Data Sains | Sophomore |
S002 | Budi | 21 | 60 | Mathematics | Junior |
S003 | Citra | 19 | 30 | Statistics | Freshman |
# 1. Create vectors for each variable
StudentID <- c("S001", "S002", "S003") # Nominal / ID
Name <- c("Alice", "Budi", "Citra") # Nominal / Name
Age <- c(20, 21, 19) # Numeric / Continuous
CreditsCompleted <- c(45, 60, 30) # Numeric / Discrete
# Nominal
Major <- c("Data Sains", "Mathematics", "Statistics")
# Ordinal
YearLevel <- factor(c("Sophomore", "Junior", "Freshman"),
levels = c("Freshman","Sophomore","Junior","Senior"),
ordered = TRUE)
# 2. Combine all vectors into a data frame
students <- data.frame(
StudentID, Name, Age, CreditsCompleted, Major, YearLevel,
stringsAsFactors = FALSE
)
# 3. Display the data frame
print(students)
## StudentID Name Age CreditsCompleted Major YearLevel
## 1 S001 Alice 20 45 Data Sains Sophomore
## 2 S002 Budi 21 60 Mathematics Junior
## 3 S003 Citra 19 30 Statistics Freshman
2 Exercise 2
Identify Data Types: Determine the type of data for each of the following variables:
# Install knitr package if not already installed
# install.packages("knitr")
library(knitr)
# Create a data frame for Data Types
variables_info <- data.frame(
No = 1:5,
Variable = c(
"Number of vehicles passing through the toll road each day",
"Student height in cm",
"Employee gender (Male / Female)",
"Customer satisfaction level: Low, Medium, High",
"Respondent's favorite color: Red, Blue, Green"
),
DataType = c(
"Quantitative",
"Quantitative",
"Quantitative",
"Qualitative",
"Qualitative"
),
Subtype = c(
"Diskrete",
"Continuous",
"Nominal",
"Ordinal",
"Nominal"
),
stringsAsFactors = FALSE
)
# Display the data frame as a neat table
kable(variables_info,
caption = "Table of Variables and Data Types")
No | Variable | DataType | Subtype |
---|---|---|---|
1 | Number of vehicles passing through the toll road each day | Quantitative | Diskrete |
2 | Student height in cm | Quantitative | Continuous |
3 | Employee gender (Male / Female) | Quantitative | Nominal |
4 | Customer satisfaction level: Low, Medium, High | Qualitative | Ordinal |
5 | Respondent’s favorite color: Red, Blue, Green | Qualitative | Nominal |
3 Exercise 3
Classify Data Sources: Determine whether the following data comes from internal or external sources, and whether it is structured or unstructured:
# Install DT package if not already installed
# install.packages("DT")
library(DT)
# Create a data frame for data sources
data_sources <- data.frame(
No = 1:4,
DataSource = c(
"Daily sales transaction data of the company",
"Weather reports from BMKG",
"Product reviews on social media",
"Warehouse inventory reports"
),
Internal_External = c(
"Internal",
"Eksternal",
"Eksternal",
"Internal"
),
Structured_Unstructured = c(
"Structured",
"Structured",
"Unstructured",
"Structured"
),
stringsAsFactors = FALSE
)
# Display the data frame as a neat table
datatable(data_sources,
caption = "Table of Data Sources",
rownames = FALSE) # hides the index column
4 Exercise 4
Dataset Structure: Consider the following transaction table:
Date | Quantity | Price | Product | CustomerTier |
---|---|---|---|---|
2025-10-01 | 2 | 1000 | Laptop | High |
2025-10-01 | 5 | 20 | Mouse | Medium |
2025-10-02 | 1 | 1000 | Laptop | Low |
2025-10-02 | 3 | 30 | Keyboard | Medium |
2025-10-03 | 4 | 50 | Mouse | Medium |
2025-10-03 | 2 | 1000 | Laptop | High |
2025-10-04 | 6 | 25 | Keyboard | Low |
2025-10-04 | 1 | 1000 | Laptop | High |
2025-10-05 | 3 | 40 | Mouse | Low |
2025-10-05 | 5 | 10 | Keyboard | Medium |
Your Assignment Instructions: Creating a Transactions Table above in R
Create a data frame in R called
transactions
containing the data above.Identify which variables are numeric and which are categorical
Calculate total revenue for each transaction by multiplying
Qty × Price
and add it as a new columnTotal
.Compute summary statistics:
- Total quantity sold for each product
- Total revenue per product
- Average price per product
Visualize the data:
- Create a barplot showing total quantity sold per product.
- Create a pie chart showing the proportion of total revenue per customer tier.
Optional Challenge:
- Find which date had the highest total revenue.
- Create a stacked bar chart showing quantity sold per product by customer tier.
Hints: Use data.frame()
,
aggregate()
, barplot()
, pie()
,
and basic arithmetic operations in R.
# Create Data Frame Transaction
library(DT)
library(knitr)
transactions <- data.frame(
No = 1:10,
Date = as.Date(c("2025-10-01", "2025-10-01","2025-10-02", "2025-10-02", "2025-10-03", "2025-10-03", "2025-10-04", "2025-10-04", "2025-10-05", "2025-10-05")),
Quantity = c(2, 5, 1, 3, 4, 2, 6, 1, 3, 5),
Price = c(1000, 20, 1000, 30, 50, 1000, 25, 1000, 40, 10),
Product = c("Laptop", "Mouse", "Laptop", "Keyboard", "Mouse", "Laptop", "Keyboard", "Laptop", "Mouse", "Keyboard"),
CustomerTier = factor(c("High", "Medium", "Low", "Medium", "Medium", "High", "Low", "High", "Low", "Medium"),
levels = c("Low", "Medium", "High"), ordered = TRUE)
)
transactions$Total <- transactions$Quantity * transactions$Price
str(transactions)
## 'data.frame': 10 obs. of 7 variables:
## $ No : int 1 2 3 4 5 6 7 8 9 10
## $ Date : Date, format: "2025-10-01" "2025-10-01" ...
## $ Quantity : num 2 5 1 3 4 2 6 1 3 5
## $ Price : num 1000 20 1000 30 50 1000 25 1000 40 10
## $ Product : chr "Laptop" "Mouse" "Laptop" "Keyboard" ...
## $ CustomerTier: Ord.factor w/ 3 levels "Low"<"Medium"<..: 3 2 1 2 2 3 1 3 1 2
## $ Total : num 2000 100 1000 90 200 2000 150 1000 120 50
#create data frame of category variable
library(DT)
categoryVariable <- data.frame(
Category = c("Date", "Quantity", "Price", "Product", "CustomersTier"),
Variable = c("Categorical (Date)",
"Numeric (Discrete)",
"Numeric(Continuous)",
"Categorical (Nominal)",
"Categorical (Ordinal)"),
stringsAsFactors = FALSE)
str(categoryVariable)
## 'data.frame': 5 obs. of 2 variables:
## $ Category: chr "Date" "Quantity" "Price" "Product" ...
## $ Variable: chr "Categorical (Date)" "Numeric (Discrete)" "Numeric(Continuous)" "Categorical (Nominal)" ...
# create data frame of total quantity
library(DT)
totalQuantity <- aggregate(Quantity ~ Product, data = transactions, sum)
str(totalQuantity)
## 'data.frame': 3 obs. of 2 variables:
## $ Product : chr "Keyboard" "Laptop" "Mouse"
## $ Quantity: num 14 6 12
datatable(totalQuantity,
rownames = FALSE,
caption = htmltools::tags$strong("Total Quantity Table")
)
# create data frame of total reveneu
library(DT)
totalRevenue <- aggregate(Total ~ Product, data = transactions, mean)
str(totalRevenue)
## 'data.frame': 3 obs. of 2 variables:
## $ Product: chr "Keyboard" "Laptop" "Mouse"
## $ Total : num 96.7 1500 140
# create data frame of avarange price
avarangePrice <- aggregate(Price ~ Product, data = transactions, mean)
str(avarangePrice)
## 'data.frame': 3 obs. of 2 variables:
## $ Product: chr "Keyboard" "Laptop" "Mouse"
## $ Price : num 21.7 1000 36.7
datatable(avarangePrice,
rownames = FALSE,
caption = htmltools::tags$strong("Avarange Price Table")
)
# create barplot of date
quantityColors <- c("#a6cba9", "#ecd59f", "#a0ced9")
barplot(totalQuantity$Quantity,
names.arg = totalQuantity$Product,
main = "Total Quantity Sold per Product",
xlab = "Product",
ylab = "Total Quantity Sold",
col = quantityColors,
border = "black")
pie_colors <- c("#cdb4db", "#eba7ac", "#a4c0d6")
revenueTier <- aggregate(Total ~ CustomerTier, data = transactions, sum)
pie(revenueTier$Total,
labels = paste(revenueTier$CustomerTier, "-", revenueTier$Total),
main = "Total Revenue by Customer Tier",
col = pie_colors)
5 Exercise 5
Create Your Own Data Frame:
Objective: Create a data frame in R with 30 rows containing a mix of data types: continuous, discrete, nominal, and ordinal.
### Instructions
Open RStudio or the R console.
Create a vector for each column in your data frame:
- Date: 30 dates (can be sequential or random within
a month/year)
- Continuous: numeric values that can take decimal
values (e.g., height, weight, temperature)
- Discrete: numeric values that can only take whole
numbers (e.g., number of items, number of vehicles)
- Nominal: categorical values with no
order (e.g., color, gender, city)
- Ordinal: categorical values with a defined order (e.g., Low, Medium, High; Beginner, Intermediate, Expert)
- Date: 30 dates (can be sequential or random within
a month/year)
Combine all vectors into a data frame called
my_data
.Check your data frame using
head()
orView()
to ensure it has 30 rows and the columns are correct.Optional tasks:
- Summarize each column using
summary()
- Count the frequency of each category for Nominal
and Ordinal columns using
table()
- Summarize each column using
### Hints
Use
seq.Date()
oras.Date()
to generate the Date column.
Use
runif()
orrnorm()
for continuous numeric data.
Use
sample()
for discrete, nominal, and ordinal data.
Ensure the ordinal vector is created with
factor(..., levels = c("Low","Medium","High"), ordered = TRUE)
(or similar).data set Structure tabel pertumbuhan tinggi badan dengan hubungan waktu tidur :
Tanggal | Jumlah Reponden | Tinggi Badan | Waktu Tidur (Jam) | Tingkat Kepuasan |
---|---|---|---|---|
2024-05-03 | 1 | 160.00 | 6 | Cukup |
2024-05-06 | 1 | 160.03 | 7 | Puas |
2024-05-09 | 1 | 160.06 | 7 | Puas |
2024-05-12 | 1 | 160.11 | 8 | Sangat Puas |
2024-05-15 | 1 | 160.12 | 6 | Cukup |
2024-05-18 | 1 | 160.12 | 7 | Puas |
2024-05-21 | 1 | 160.15 | 5 | Kurang Puas |
2024-05-24 | 1 | 160.15 | 7 | Puas |
2024-05-27 | 1 | 160.18 | 8 | Sangat Puas |
2024-05-30 | 1 | 160.23 | 7 | Puas |
2024-06-03 | 1 | 160.26 | 6 | Cukup |
2024-06-06 | 1 | 160.27 | 7 | Puas |
2024-06-09 | 1 | 160.30 | 5 | Kurang Puas |
2024-06-12 | 1 | 160.35 | 8 | Sangat Puas |
2024-06-15 | 1 | 160.38 | 7 | Puas |
2024-06-18 | 1 | 160.43 | 8 | Sangat Puas |
2024-06-21 | 1 | 160.44 | 6 | Cukup |
2024-06-24 | 1 | 160.47 | 7 | Puas |
2024-06-27 | 1 | 160.47 | 5 | Kurang Puas |
2024-06-30 | 1 | 160.52 | 8 | Sangat Puas |
2024-07-03 | 1 | 160.55 | 7 | Puas |
2024-07-06 | 1 | 160.56 | 6 | Cukup |
2024-07-09 | 1 | 160.59 | 7 | Puas |
2024-07-12 | 1 | 160.64 | 8 | Sangat Puas |
2024-07-15 | 1 | 160.65 | 6 | Cukup |
2024-07-18 | 1 | 160.68 | 7 | Puas |
2024-07-21 | 1 | 160.73 | 8 | Sangat Puas |
2024-07-24 | 1 | 160.76 | 7 | Puas |
2024-07-27 | 1 | 160.76 | 5 | Kurang Puas |
2024-07-30 | 1 | 160.81 | 8 | Sangat Puas |
# Create Data Frame "pertumbuhan tinggi badan" table
set.seed(123) # tetap ada seed untuk reproducibility
my_data <- data.frame(
No = 1:30,
Tanggal = seq.Date(as.Date("2024-05-03"), by = "3 day", length.out = 30),
jumlah_responden = rep(1, 30),
tinggi_badan = round(160 + (0:29) * ((160.81 - 160.00) /29) + runif(30,
min = 0, max = 0.0001), 2),
waktu_tidur = factor(c(6,7,7,8,6,7,5,7,8,7,6,7,5,8,7,8,6,7,5,8,7,6,7,8,6,7,8,7,5,8)),
tingkat_kepuasan = factor(c("Cukup", "Puas", "Puas", "Sangat Puas", "Cukup", "Puas", "Kurang Puas", "Puas", "Sangat Puas", "Puas", "Cukup", "Puas", "Kurang Puas", "Sangat Puas","Puas", "Sangat Puas", "Cukup", "Puas", "Kurang Puas", "Sangat Puas", "Puas", "Cukup", "Puas", "Sangat Puas", "Cukup", "Puas", "Sangat Puas", "Puas", "Kurang Puas", "Sangat Puas"),
levels = c("Kurang Puas", "Cukup", "Puas", "Sangat Puas"),
ordered = TRUE)
)
head(my_data, 30, row.names = FALSE)
## No Tanggal jumlah_responden tinggi_badan
## Min. : 1.00 Min. :2024-05-03 Min. :1 Min. :160.0
## 1st Qu.: 8.25 1st Qu.:2024-05-24 1st Qu.:1 1st Qu.:160.2
## Median :15.50 Median :2024-06-15 Median :1 Median :160.4
## Mean :15.50 Mean :2024-06-15 Mean :1 Mean :160.4
## 3rd Qu.:22.75 3rd Qu.:2024-07-07 3rd Qu.:1 3rd Qu.:160.6
## Max. :30.00 Max. :2024-07-29 Max. :1 Max. :160.8
## waktu_tidur tingkat_kepuasan
## 5: 4 Kurang Puas: 4
## 6: 6 Cukup : 6
## 7:12 Puas :12
## 8: 8 Sangat Puas: 6
## NA's : 2
##
##
## Kurang Puas Cukup Puas Sangat Puas
## 4 6 12 6