flowchart TD A[Import data] --> B[Explore data] B --> C[Identify features for scaling] C--> D[Apply scaling] D --> E[Visualise and review] classDef level1 fill:#add8e6,stroke:#333,stroke-width:1px,font-weight:normal; classDef level2 fill:#fffacd,stroke:#333,stroke-width:1px,font-weight:normal; class A,B,C,D,E level1;
Data Transformation - Practical
Process
Data Import
Task
Download the dataset here:
Solution
Code
# Load the dataset
sports_data <- read.csv("https://www.dropbox.com/scl/fi/hrwuzi5kl3ftj4rmal5ew/scaling_practice.csv?rlkey=h94fsw9yp3mfw9yxlugq41qlw&dl=1")Data Exploration
Objective: To understand the structure and summary of the dataset.
Tasks
- Inspect the structure of the dataset to identify variable types.
- Summarise the dataset to understand the ranges and distributions of variables.
Solutions
Code
# Inspect structure
str(sports_data)
# Summary
summary(sports_data)Identify Variables for Scaling
Objective: To determine which numeric variables are suitable for scaling.
Tasks
- Identify numeric variables in the dataset that require scaling (e.g., using logical indexing).
- Exclude non-numeric or categorical variables (such as
Athlete_IDandSport). - Create a subset of the dataset containing only the numeric variables to be scaled.
Solutions
Code
# Identify numeric variables
numeric_vars <- sports_data[, sapply(sports_data, is.numeric)]
# Exclude categorical variables
numeric_vars <- numeric_vars[, !names(numeric_vars) %in% c("Athlete_ID")]
# Display selected variables
names(numeric_vars)Apply Scaling
Objective: To standardise numeric variables to have a mean of 0 and a standard deviation of 1.
Tasks
- Scale the numeric variables using the
scale()function. - Combine the scaled variables with the original dataset.
- Ensure the combined dataset includes indicators for scaled variables.
Solutions
Code
# Scale the numeric variables
scaled_vars <- as.data.frame(scale(numeric_vars))
# Combine scaled variables with original dataset
sports_data_scaled <- cbind(sports_data, scaled_vars)
# Preview the combined dataset
head(sports_data_scaled)Visualise and Review Scaled Data
Objective: To compare the original and scaled data.
Tasks
- Generate summary statistics for the scaled variables.
- Create boxplots to visualise distribution of original and scaled variables.
- Compare and interpret the distributions.
Solutions
Code
library(reshape2) # needed for final figure
library(ggplot2)
# Summary statistics of scaled variables
summary(scaled_vars)
# Visualise original vs scaled data (example: Height_cm)
boxplot(sports_data$Height_cm, scaled_vars$Height_cm,
names = c("Original Height", "Scaled Height"),
main = "Comparison of Original and Scaled Height")Code
# Visualise original and scaled data separately
# Melt the original and scaled datasets for plotting
original_melt <- melt(numeric_vars, variable.name = "Variable", value.name = "Original")
scaled_melt <- melt(scaled_vars, variable.name = "Variable", value.name = "Scaled")
# Plot original variables
original_plot <- ggplot(original_melt, aes(x = Variable, y = Original)) +
geom_boxplot(fill = "blue", alpha = 0.5) +
labs(title = "Original Variables", y = "Value", x = "Variable") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Plot scaled variables
scaled_plot <- ggplot(scaled_melt, aes(x = Variable, y = Scaled)) +
geom_boxplot(fill = "green", alpha = 0.5) +
labs(title = "Scaled Variables", y = "Value", x = "Variable") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Display the plots side by side
library(gridExtra)
grid.arrange(original_plot, scaled_plot, ncol = 2)