Data Analytics with R — Course Notes
Data Analytics with R
Complete Detailed Course Notes
Units I – V | Introduction to R, Data Handling, Manipulation, Summarization & Case Studies
1 of 25
Data Analytics with R — Course Notes
UNIT I — Introduction to Data Analytics and R
1.1 Introduction to Data Analytics
Data Analytics is the science of examining raw data with the purpose of drawing conclusions, identifying
patterns, and supporting decision-making. Organizations today generate enormous volumes of data from
transactions, sensors, social media, and applications, and Data Analytics provides the methods and tools to
convert this raw data into actionable insight.
Formally, Data Analytics can be defined as the process of inspecting, cleansing, transforming, and
modelling data with the goal of discovering useful information, informing conclusions, and supporting
decision-making.
Why Data Analytics matters
• Helps organizations understand customer behaviour and market trends.
• Improves operational efficiency by identifying bottlenecks and waste.
• Supports evidence-based decision making instead of intuition-based decisions.
• Enables prediction of future outcomes (sales, risk, failures) using historical data.
• Provides a competitive advantage by uncovering hidden patterns competitors may miss.
1.2 Data Analysis vs Data Analytics
Although the terms are often used interchangeably, they represent related but distinct concepts.
Aspect Data Analysis Data Analytics
Definition The process of inspecting and The broader science/discipline that
examining existing data to find encompasses tools, techniques and
specific answers to specific questions. processes used to examine data,
including analysis.
Scope Narrower — focused on examining a Broader — includes collection,
given dataset. cleaning, transformation, modelling
and interpretation.
Orientation Retrospective — explains what Can be retrospective, predictive, or
happened. prescriptive.
Tools used Spreadsheets, SQL queries, simple R, Python, Machine Learning, Big
statistics. Data platforms, BI tools.
Outcome A report or answer to a specific Insights, models, and strategic
question. decisions/recommendations.
Example Finding the average monthly sales of Building a model to forecast next
a product. year's sales and recommend
inventory strategy.
In short: Data Analysis is a subset of Data Analytics. Analysis answers 'what happened', while Analytics as a
discipline also asks 'why it happened' and 'what will happen next'.
1.3 Types of Data Analytics
Data Analytics is generally classified into four major types, often visualized as increasing in complexity and
business value:
1. Descriptive Analytics
• Answers: 'What happened?'
2 of 25
Data Analytics with R — Course Notes
• Summarizes historical data using statistics, dashboards, and reports.
• Example: Monthly sales report, website traffic summary.
2. Diagnostic Analytics
• Answers: 'Why did it happen?'
• Involves drill-down, data discovery, correlations and root-cause analysis.
• Example: Analyzing why sales dropped in a particular region last quarter.
3. Predictive Analytics
• Answers: 'What is likely to happen?'
• Uses statistical models and machine learning on historical data to forecast future outcomes.
• Example: Predicting customer churn or forecasting next quarter's demand.
4. Prescriptive Analytics
• Answers: 'What should we do about it?'
• Recommends actions based on predictive outputs, using optimization and simulation.
• Example: Recommending optimal pricing or inventory allocation strategy.
Note: Descriptive and diagnostic analytics look backward (hindsight); predictive and prescriptive
analytics look forward (foresight). Complexity and business value generally increase from descriptive to
prescriptive.
1.4 Data Analytics Framework
A Data Analytics framework describes the systematic sequence of steps followed to convert raw data into
insight. A typical framework includes the following stages:
1. Business/Problem Understanding — Define the objective, questions to be answered, and success
criteria.
2. Data Collection — Gather data from relevant sources (databases, APIs, sensors, files, web).
3. Data Pre-processing / Cleaning — Handle missing values, remove duplicates, correct inconsistent
formats.
4. Data Exploration (EDA) — Use summary statistics and visualizations to understand data distribution
and relationships.
5. Data Transformation — Normalize, scale, encode, or aggregate data as required by the analysis
method.
6. Modelling / Analysis — Apply statistical or machine learning techniques appropriate to the
problem (descriptive, predictive, etc.).
7. Evaluation — Validate the model/analysis using appropriate metrics and check whether the
objective is met.
8. Deployment / Reporting — Present results through dashboards, reports, or integrate the model
into production systems.
9. Monitoring & Feedback — Continuously track performance and refine the analysis as new data
arrives.
This is conceptually similar to well-known frameworks such as CRISP-DM (Cross Industry Standard Process
for Data Mining), which cycles through Business Understanding → Data Understanding → Data Preparation
→ Modelling → Evaluation → Deployment.
3 of 25
Data Analytics with R — Course Notes
1.5 Data Analytics Tools
A wide variety of tools support different stages of the analytics process. Some of the most widely used are:
Tool Category Key Strength
R Statistical programming language
Rich statistical & graphical capability,
large package ecosystem (CRAN)
Python General-purpose programming Versatile, strong ML/AI libraries
language (pandas, scikit-learn)
Excel Spreadsheet tool Easy to use, good for quick analysis of
small data
Tableau / Power BI Business Intelligence / Interactive dashboards, drag-and-
Visualization drop visualization
SQL Query language Efficient querying and aggregation of
relational data
SAS Statistical software suite Enterprise-grade statistical analysis
Hadoop / Spark Big Data platforms Distributed processing of very large
datasets
This course focuses on R, one of the most popular open-source languages specifically designed for
statistical computing and graphics.
1.6 The R Language — Understanding R
R is a free, open-source programming language and software environment used primarily for statistical
computing, data analysis, and graphical representation of data. It was created by Ross Ihaka and Robert
Gentleman at the University of Auckland, New Zealand, in the early 1990s, and is currently developed by
the R Development Core Team.
R is an implementation of the S programming language combined with lexical scoping semantics inspired
by Scheme. It is widely used among statisticians, data miners, and data scientists for developing statistical
software and performing data analysis.
Features of R
• Open Source and Free — R can be freely downloaded, used, and modified under the GNU General
Public License.
• Platform Independent — R runs on Windows, macOS, and Linux.
• Comprehensive Statistical Capability — Provides a wide variety of statistical techniques: linear and
nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, etc.
• Excellent Graphics — R produces publication-quality plots including mathematical symbols and
formulae.
• Extensible via Packages — Over 18,000 packages are available on CRAN (Comprehensive R Archive
Network) covering almost every statistical and data-science domain.
• Active Community Support — A large, active global community continuously contributes packages,
tutorials, and support forums.
• Interoperability — R can interface with C, C++, Java, Python, and databases, and can import/export
many file formats (CSV, Excel, JSON, XML, etc.).
• Vectorized Operations — Operations are applied to entire vectors/arrays without explicit loops,
making code concise and efficient.
• Reproducible Research — Integrates well with tools like R Markdown and Shiny for reproducible
reports and interactive web applications.
4 of 25
Data Analytics with R — Course Notes
Installing R and RStudio
R is the underlying language/engine, while RStudio is a popular Integrated Development Environment (IDE)
that makes working with R easier through a friendlier interface (script editor, console, environment viewer,
plot viewer, all in one window).
Steps to install R:
10. Visit the official CRAN website: [Link]
11. Select the operating system (Windows / macOS / Linux).
12. Download the latest stable version of R (the 'base' distribution).
13. Run the installer and follow the setup wizard, accepting default settings.
14. Once installed, R can be launched via the R console (Rgui on Windows, or terminal on Linux/Mac).
Steps to install RStudio:
15. Visit [Link] (RStudio Desktop, free version).
16. Download the installer appropriate for your operating system.
17. Ensure R is already installed, since RStudio requires an existing R installation to run against.
18. Run the installer and complete the setup.
19. Launch RStudio — it automatically detects the installed R engine.
RStudio interface — four main panes
• Source/Script Editor — for writing and saving R scripts (.R files).
• Console — where commands are typed and executed interactively.
• Environment/History — shows currently loaded variables, data frames, and command history.
• Files/Plots/Packages/Help — file browser, plot viewer, package manager, and help documentation.
1.7 Packages and Library in R
A package in R is a collection of functions, data, compiled code, and documentation bundled together to
extend the base capability of R. R's power largely comes from its huge repository of contributed packages.
Key repositories
• CRAN (Comprehensive R Archive Network) — the official and most common repository.
• Bioconductor — repository specialized for bioinformatics packages.
• GitHub — many developers host packages here; installed using the 'devtools' or 'remotes' package.
Common commands for package management
# Install a package from CRAN
[Link]("dplyr")
# Load (attach) an installed package into the session
library(dplyr)
# Check which packages are currently loaded
search()
# List all installed packages
[Link]()
# Update all installed packages
[Link]()
5 of 25
Data Analytics with R — Course Notes
# Remove/uninstall a package
[Link]("dplyr")
# Get help about a package
help(package = "dplyr")
The distinction between [Link]() and library() is important: [Link]() downloads and
installs the package files onto the computer (done once), whereas library() loads the package into the
current R session so its functions become available for use (needed every session).
Some commonly used R packages
Package Purpose
dplyr Data manipulation (select, filter, mutate, arrange, summarise)
ggplot2 Advanced data visualization based on the Grammar of Graphics
tidyr Data tidying/reshaping
readr / readxl Fast reading of CSV / Excel files
jsonlite Reading and writing JSON files
XML / xml2 Reading and writing XML files
caret Machine learning model training and evaluation
shiny Building interactive web applications
Unit I — Summary
• Data Analytics converts raw data into actionable insight; Data Analysis is a narrower, question-
answering subset of it.
• Four types of analytics: Descriptive, Diagnostic, Predictive, and Prescriptive.
• A typical analytics framework moves from problem understanding through data collection,
cleaning, exploration, modelling, evaluation, and deployment.
• R is a free, open-source statistical programming language known for its rich packages and graphics.
• RStudio is an IDE that makes working with R more convenient.
• Packages extend R's functionality; [Link]() installs them once, library() loads them each
session.
6 of 25
Data Analytics with R — Course Notes
UNIT II — Importing and Exporting Files, Data Pre-Processing
2.1 Introduction
Before any analysis can be performed, data must be brought into R from an external source (importing)
and, after processing, results often need to be saved back out (exporting). R supports a wide variety of file
formats including CSV, JSON, plain text, Excel, and XML. This unit also covers the essential data pre-
processing steps — handling missing values, transformation, selection, and integration — that prepare raw
data for analysis.
2.2 CSV File
CSV (Comma-Separated Values) is the most common format for tabular data exchange because it is simple,
lightweight, and supported by virtually every data tool.
Importing a CSV file
# Base R function
data <- [Link]("[Link]", header = TRUE, sep = ",")
# Using readr package (faster, tidyverse-friendly)
library(readr)
data <- read_csv("[Link]")
# View first few rows
head(data)
str(data) # structure of the data frame
Exporting a CSV file
# Base R
[Link](data, "[Link]", [Link] = FALSE)
# readr package
library(readr)
write_csv(data, "[Link]")
2.3 JSON File
JSON (JavaScript Object Notation) is a lightweight, human-readable data-interchange format widely used in
web APIs, often containing nested/hierarchical structures. The 'jsonlite' package is commonly used to work
with JSON in R.
library(jsonlite)
# Importing JSON
data <- fromJSON("[Link]")
# Exporting a data frame to JSON
toJSON(data, pretty = TRUE)
write(toJSON(data, pretty = TRUE), file = "[Link]")
2.4 Text (.txt) File
Plain text files store data without special formatting; they may be delimited (tab, space, etc.) or
unstructured.
7 of 25
Data Analytics with R — Course Notes
# Reading a delimited text file
data <- [Link]("[Link]", header = TRUE, sep = "\t")
# Reading line by line (unstructured text)
lines <- readLines("[Link]")
# Writing to a text file
[Link](data, "[Link]", sep = "\t", [Link] = FALSE)
writeLines(lines, "[Link]")
2.5 Excel File
Excel files (.xls, .xlsx) are widely used in business settings. The 'readxl' package is used for reading, and
'writexl' or 'openxlsx' for writing, without requiring Excel to be installed.
library(readxl)
data <- read_excel("[Link]", sheet = 1)
library(writexl)
write_xlsx(data, "[Link]")
# openxlsx offers more formatting control
library(openxlsx)
[Link](data, "[Link]")
2.6 XML File
XML (eXtensible Markup Language) stores data in a nested, tag-based hierarchical structure, commonly
used in configuration files and data interchange between systems. The 'XML' or 'xml2' packages handle
XML parsing in R.
library(XML)
xml_data <- xmlParse("[Link]")
xml_df <- xmlToDataFrame(xml_data)
# Using xml2 (tidyverse-style)
library(xml2)
doc <- read_xml("[Link]")
xml_structure(doc)
2.7 Command Line vs Scripts
Aspect Command Line (Console) Scripts (.R files)
Definition Typing and executing R commands Writing a sequence of R commands
one at a time directly in the in a file (.R) and executing them
R/RStudio console. together.
Use case Quick, one-off checks; testing small Repeatable analysis; larger projects;
snippets; interactive exploration. reproducible workflows.
Persistence Commands are not saved Saved permanently as a file; can be
automatically (though history can be reused, shared, and version-
recalled). controlled.
Debugging Harder to debug multi-step logic. Easier to debug, comment, and
modify systematically.
Reproducibility Low — difficult for others to repeat High — anyone can re-run the script
exact steps. for the same result.
Note: In practice, the console is used for quick exploration while a script is used to build the final,
reproducible analysis pipeline that can be re-run or shared with others.
8 of 25
Data Analytics with R — Course Notes
2.8 Data Pre-Processing
Real-world data is often incomplete, inconsistent, and noisy. Data pre-processing is the set of techniques
used to convert raw data into a clean, consistent format suitable for analysis. It is one of the most time-
consuming yet critical stages of any analytics project — commonly estimated to take up 60-80% of an
analyst's effort.
Major pre-processing steps
20. Data Cleaning — handling missing values, correcting errors, removing duplicates/outliers.
21. Data Transformation — normalization, scaling, encoding, aggregation.
22. Data Reduction — dimensionality reduction, feature selection.
23. Data Integration — combining data from multiple sources into a coherent dataset.
2.9 Missing Values
Missing values occur when no data value is stored for a variable in an observation. In R, missing values are
represented by the special symbol NA (Not Available).
Detecting missing values
# Check for NA values
[Link](data)
sum([Link](data)) # total count of NAs
colSums([Link](data)) # NA count per column
which([Link](data$column)) # positions of NA in a column
Handling missing values
• Omission/Deletion — remove rows or columns containing NA values (used when missing data is
small in proportion).
• Imputation — replace missing values with a substitute such as mean, median, mode, or a predicted
value.
• Flagging — create an indicator variable marking which values were originally missing, retaining the
record.
2.10 Omitting Null Values
# Remove rows with any NA values
clean_data <- [Link](data)
# Remove rows where a specific column has NA
clean_data <- data[, ]
# [Link]() identifies rows without any NA
clean_data <- data[[Link](data), ]
# Replace NA with mean (imputation example)
data$column[[Link](data$column)] <- mean(data$column, [Link] = TRUE)
Note: [Link] = TRUE must be used inside functions like mean(), sum(), sd() etc. whenever the vector
may contain NA — otherwise the function itself returns NA.
2.11 Data Transformation
Data transformation converts data from one format or structure to another so that it is suitable for
analysis. Common transformation techniques include:
9 of 25
Data Analytics with R — Course Notes
• Normalization — rescaling numeric values to a common range, typically [0, 1].
• Standardization (Z-score scaling) — rescaling data to have mean 0 and standard deviation 1.
• Log/Power Transformation — reducing skewness in data with a long tail.
• Encoding categorical variables — converting text categories into numeric/factor codes (e.g.,
[Link](), one-hot encoding).
• Aggregation — summarizing data at a higher level (e.g., daily sales aggregated to monthly).
# Normalization (min-max scaling)
normalize <- function(x) (x - min(x)) / (max(x) - min(x))
data$norm_col <- normalize(data$column)
# Standardization
data$z_col <- scale(data$column)
# Log transformation
data$log_col <- log(data$column + 1)
# Encoding a categorical variable as factor
data$category <- [Link](data$category)
2.12 Data Selection
Data selection involves choosing the relevant subset of rows and/or columns needed for a specific analysis,
discarding irrelevant or redundant data.
# Select specific columns by name
subset_data <- data[, c("col1", "col2")]
# Select rows meeting a condition
subset_data <- data[data$column > 50, ]
# Using base subset() function
subset_data <- subset(data, column > 50, select = c(col1, col2))
2.13 Data Integration
Data integration combines data from multiple, often heterogeneous, sources (different files, databases,
formats) into a single, unified dataset for analysis. Key challenges include entity resolution (identifying that
records from different sources refer to the same entity), schema matching, and resolving data value
conflicts.
Common integration operations in R
# Merging two data frames on a common key column (like SQL JOIN)
merged_data <- merge(df1, df2, by = "id")
# Left join, right join, full outer join
left_join <- merge(df1, df2, by = "id", all.x = TRUE)
right_join <- merge(df1, df2, by = "id", all.y = TRUE)
full_join <- merge(df1, df2, by = "id", all = TRUE)
# Row-binding (stacking datasets with the same columns)
combined <- rbind(df1, df2)
# Column-binding (side by side, same number of rows)
combined <- cbind(df1, df2)
Unit II — Summary
10 of 25
Data Analytics with R — Course Notes
• R can import/export data from CSV, JSON, TXT, Excel, and XML formats using dedicated
functions/packages.
• Scripts provide reproducibility over one-off command-line execution.
• Missing values (NA) must be detected and handled through omission or imputation before analysis.
• Data transformation (normalization, standardization, encoding) prepares data for modelling.
• Data selection filters relevant rows/columns; data integration merges multiple sources into one
dataset.
11 of 25
Data Analytics with R — Course Notes
UNIT III — Data Manipulation
3.1 Introduction to Data Manipulation
Data manipulation refers to the process of adjusting, organizing, and restructuring data to make it more
readable, useful, or ready for analysis. In R, data manipulation is performed using base R indexing
techniques (slicing, subscripts) as well as specialized packages such as dplyr, which provide a clean,
readable 'grammar' for common data operations.
3.2 Slicing
Slicing refers to extracting a specific portion (subset) of a vector, matrix, or data frame based on position or
condition.
v <- c(10, 20, 30, 40, 50)
# Slice elements 2 to 4
v[2:4] # 20 30 40
# Slice a data frame - rows 1 to 3, columns 1 to 2
df[1:3, 1:2]
# Slice all rows for a specific column
df[, "col1"]
# Slice using negative indices (exclude elements)
v[-1] # exclude first element
df[-c(1,2), ] # exclude first two rows
3.3 Subscripts and Indices
R uses square brackets [ ] for indexing. Unlike many other languages, R indexing starts at 1, not 0.
Types of indexing in R
• Positive indexing — v[3] returns the 3rd element.
• Negative indexing — v[-3] returns all elements except the 3rd.
• Logical indexing — v[v > 20] returns elements satisfying a condition.
• Named indexing — for named vectors/lists, elements can be accessed by name, e.g. v['a'].
v <- c(a=10, b=20, c=30)
v[2] # positional -> 20 (named 'b')
v[-2] # exclude 2nd element
v[v > 15] # logical indexing -> 20 30
v["b"] # named indexing -> 20
# Data frame double vs single bracket
df[1] # returns a data frame (single column, still [Link] class)
df[[1]] # returns a vector (the actual column contents)
df$col1 # returns a vector by column name (most common)
3.4 Data Subset
The subset() function in base R provides a convenient way to select rows and columns of a data frame
using logical conditions, without needing repeated use of df$ notation.
12 of 25
Data Analytics with R — Course Notes
# Basic subset: rows where age > 25
subset(df, age > 25)
# Subset with column selection
subset(df, age > 25, select = c(name, age, salary))
# Subset with multiple conditions
subset(df, age > 25 & department == "Sales")
# Excluding columns using minus sign
subset(df, select = -c(id))
Note: subset() is convenient for interactive use, but the dplyr package (see below) is generally
preferred in modern R programming as it is more consistent, readable, and works well within data
pipelines.
3.5 The dplyr Package
dplyr is one of the most widely used R packages for data manipulation, part of the tidyverse collection. It
provides a small, consistent set of 'verbs' (functions) that each perform one well-defined data manipulation
task, which can be chained together to build complex transformations in a readable way, typically using the
pipe operator %>% or the native pipe |>.
library(dplyr)
The five core dplyr verbs covered in this unit are select(), filter(), mutate(), and arrange() (plus summarise(),
covered together with grouping in Unit IV).
3.5.1 select() — Choosing Columns
The select() function is used to pick a subset of columns from a data frame by name or position.
library(dplyr)
# Select specific columns
select(df, name, age, salary)
# Select a range of columns
select(df, name:salary)
# Exclude a column
select(df, -id)
# Select columns matching a pattern
select(df, starts_with("sal"))
select(df, ends_with("date"))
select(df, contains("name"))
3.5.2 filter() — Choosing Rows
The filter() function extracts rows that satisfy specified logical conditions, similar to a SQL WHERE clause.
# Rows where age is greater than 30
filter(df, age > 30)
# Multiple conditions (AND)
filter(df, age > 30 & department == "IT")
# Multiple conditions (OR)
filter(df, age > 30 | salary > 50000)
13 of 25
Data Analytics with R — Course Notes
# Using %in% for multiple matching values
filter(df, department %in% c("IT", "Sales"))
3.5.3 mutate() — Creating/Modifying Columns
The mutate() function adds new columns or modifies existing columns, typically as a function of other
columns, while keeping all existing columns intact.
# Add a new column
mutate(df, bonus = salary * 0.10)
# Add multiple columns at once
mutate(df, bonus = salary * 0.10, total = salary + bonus)
# Modify an existing column
mutate(df, salary = salary * 1.05) # 5% salary hike
3.5.4 arrange() — Sorting Rows
The arrange() function reorders the rows of a data frame based on the values of one or more columns.
# Ascending order (default)
arrange(df, salary)
# Descending order
arrange(df, desc(salary))
# Sort by multiple columns
arrange(df, department, desc(salary))
3.6 Chaining Operations with the Pipe Operator
The real strength of dplyr emerges when multiple verbs are chained together using the pipe operator (%>%
from magrittr, or the base R native pipe |>), which passes the result of one step as the input to the next,
avoiding nested function calls and improving readability.
library(dplyr)
result <- df %>%
filter(department == "IT") %>%
select(name, age, salary) %>%
mutate(bonus = salary * 0.10) %>%
arrange(desc(salary))
print(result)
3.7 Summary Table of dplyr Verbs
Function Purpose SQL Equivalent
select() Choose columns SELECT column_list
filter() Choose rows based on condition WHERE clause
mutate() Add/modify columns SELECT ... AS (computed
column)
arrange() Sort rows ORDER BY
summarise() Aggregate/summarize data GROUP BY + aggregate
functions
14 of 25
Data Analytics with R — Course Notes
Unit III — Summary
• Slicing and subscripting (positive, negative, logical, and named indexing) are fundamental base-R
techniques for extracting subsets of data.
• subset() offers a convenient base-R way to filter rows/columns using logical conditions.
• dplyr provides consistent, readable verbs: select() for columns, filter() for rows, mutate() for
new/modified columns, and arrange() for sorting.
• The pipe operator (%>% or |>) allows multiple dplyr verbs to be chained into a clear, sequential
data-manipulation pipeline.
15 of 25
Data Analytics with R — Course Notes
UNIT IV — Data Summarization and Visualization
4.1 Introduction
Data summarization refers to the use of descriptive statistics to condense a dataset into a small number of
representative values, making it easier to understand overall patterns, central tendency, and spread. This
unit covers measures of central tendency (mean, median, mode), measures of variability (variance, range,
IQR, standard deviation, sum of squares), and the use of the IQR method to identify outliers.
4.2 Measures of Central Tendency
Measures of central tendency describe the 'centre' or typical value of a dataset.
4.2.1 Mean
The mean (arithmetic average) is the sum of all values divided by the number of values. It is the most
commonly used measure of central tendency but is sensitive to extreme values (outliers).
x <- c(10, 20, 30, 40, 50)
mean(x) # 30
mean(x, [Link] = TRUE) # ignoring NA values
4.2.2 Median
The median is the middle value of a dataset when arranged in ascending order. If there is an even number
of observations, it is the average of the two middle values. The median is robust to outliers, making it a
better measure of centre for skewed data.
x <- c(10, 20, 30, 40, 50)
median(x) # 30
y <- c(10, 20, 30, 40)
median(y) # (20+30)/2 = 25
4.2.3 Mode
The mode is the value that occurs most frequently in a dataset. Unlike mean and median, R does not have
a built-in mode() function for statistical mode (R's mode() function actually returns the data type), so a
custom function is typically used.
get_mode <- function(v) {
uniq_v <- unique(v)
uniq_v[[Link](tabulate(match(v, uniq_v)))]
}
x <- c(1, 2, 2, 3, 3, 3, 4)
get_mode(x) # 3
Measure Definition Sensitive to Best Used For
Outliers?
Mean Sum of values / count of values Yes Symmetric,
normally-
distributed
numeric data
Median Middle value of sorted data No Skewed data,
presence of
outliers
Mode Most frequently occurring value No Categorical data /
16 of 25
Data Analytics with R — Course Notes
Measure Definition Sensitive to Best Used For
Outliers?
finding most
common value
4.3 Measures of Variability (Dispersion)
While central tendency describes the 'typical' value, measures of variability describe how spread out the
data values are around that centre.
4.3.1 Range
The range is the simplest measure of spread: the difference between the maximum and minimum values in
the dataset.
x <- c(10, 20, 30, 40, 50)
range(x) # 10 50 (min and max)
max(x) - min(x) # 40 (the actual range value)
4.3.2 Variance
Variance measures the average of the squared differences (deviations) from the mean. It quantifies how
far each value in the dataset is from the mean, on average, in squared units.
Formula (sample variance): s² = Σ(xᵢ − x̄ )² / (n − 1)
x <- c(10, 20, 30, 40, 50)
var(x) # 250
4.3.3 Standard Deviation
Standard deviation is the square root of the variance. It expresses spread in the same unit as the original
data (unlike variance, which is in squared units), making it more interpretable.
Formula: s = √(variance)
x <- c(10, 20, 30, 40, 50)
sd(x) # 15.81139
sqrt(var(x)) # same result, computed manually
4.3.4 Interquartile Range (IQR)
The IQR measures the spread of the middle 50% of the data. It is calculated as the difference between the
third quartile (Q3, 75th percentile) and the first quartile (Q1, 25th percentile). IQR is a robust measure of
spread, unaffected by extreme outliers.
Formula: IQR = Q3 − Q1
x <- c(10, 20, 30, 40, 50, 60, 70)
IQR(x) # Interquartile range
quantile(x, 0.25) # Q1
quantile(x, 0.75) # Q3
summary(x) # gives Min, Q1, Median, Mean, Q3, Max at once
4.3.5 Sum of Squares
Sum of Squares (SS) is the sum of the squared deviations of each data point from the mean. It is a
foundational quantity used in the calculation of variance, standard deviation, regression analysis, and
ANOVA.
Formula: SS = Σ(xᵢ − x̄ )²
x <- c(10, 20, 30, 40, 50)
ss <- sum((x - mean(x))^2)
17 of 25
Data Analytics with R — Course Notes
ss # 1000
# Relationship to variance: variance = SS / (n-1)
ss / (length(x) - 1) # 250, matches var(x)
Measure Formula Unit Robust to
Outliers?
Range Max − Min Same as data No
Variance Σ(x−x̄ )² / (n−1) Squared units No
Standard Deviation √Variance Same as data No
IQR Q3 − Q1 Same as data Yes
Sum of Squares Σ(x−x̄ )² Squared units No
4.4 Identifying Outliers using IQR
An outlier is a data point that differs significantly from other observations in the dataset. The IQR method is
a widely used, robust technique for detecting outliers because it does not rely on the mean or standard
deviation (which are themselves affected by outliers).
IQR method rule
A data point is considered an outlier if it lies below the Lower Bound or above the Upper Bound, defined
as:
• Lower Bound = Q1 − 1.5 × IQR
• Upper Bound = Q3 + 1.5 × IQR
x <- c(10, 12, 14, 15, 16, 18, 19, 20, 100) # 100 is a potential outlier
Q1 <- quantile(x, 0.25)
Q3 <- quantile(x, 0.75)
iqr_val <- IQR(x)
lower_bound <- Q1 - 1.5 * iqr_val
upper_bound <- Q3 + 1.5 * iqr_val
# Identify outliers
outliers <- x[x < lower_bound | x > upper_bound]
outliers # 100
# Visualize outliers using a boxplot
boxplot(x, main = "Boxplot showing outlier(s)", horizontal = TRUE)
Note: In a boxplot, the box represents Q1 to Q3 (the IQR), the line inside the box is the median, the
'whiskers' extend to the lower/upper bound (or to the min/max within that bound), and any points
beyond the whiskers are plotted individually as outliers.
4.5 Data Visualization in R
Visualization complements summarization by showing the shape, spread, and relationships in data
graphically. Base R and the ggplot2 package are commonly used.
# Histogram - shows distribution shape
hist(x, main = "Histogram", xlab = "Value", col = "skyblue")
# Boxplot - shows median, quartiles, and outliers
boxplot(x, main = "Boxplot")
# Scatter plot - shows relationship between two variables
18 of 25
Data Analytics with R — Course Notes
plot(df$var1, df$var2, main = "Scatter Plot")
# Bar plot - for categorical data frequencies
barplot(table(df$category))
# Using ggplot2 for more polished visuals
library(ggplot2)
ggplot(df, aes(x = category, y = value)) +
geom_boxplot() +
theme_minimal()
Unit IV — Summary
• Mean, median, and mode measure central tendency; mean is sensitive to outliers, median and
mode are more robust.
• Range, variance, standard deviation, IQR, and sum of squares measure the spread/variability of
data.
• IQR (Q3 − Q1) is a robust dispersion measure representing the middle 50% of data.
• Outliers are detected using the rule: value < Q1 − 1.5×IQR or value > Q3 + 1.5×IQR.
• Boxplots are the standard visualization for showing quartiles and outliers together.
19 of 25
Data Analytics with R — Course Notes
UNIT V — Data Analytics Case Studies
5.1 Introduction
This unit examines how the concepts covered in earlier units — data import, pre-processing, manipulation,
and summarization/visualization — are applied to real-world business domains. Each case study outlines
the business problem, the type of data typically involved, the analytical approach used, and the value
delivered.
5.2 Case Study: Marketing Analytics
Business Problem
Marketing teams need to understand which campaigns, channels, and customer segments generate the
highest return on investment, and how to target the right customers with the right message.
Typical Data Sources
• Customer demographic data (age, location, income).
• Campaign data (channel, spend, impressions, clicks, conversions).
• Web/app analytics (page views, session duration, bounce rate).
• Purchase/transaction history.
Analytical Approach
24. Descriptive analytics: Summarize campaign performance (CTR, conversion rate) by channel and
segment.
25. Customer segmentation: Group customers using clustering techniques (e.g., k-means) based on
behaviour/demographics.
26. RFM Analysis (Recency, Frequency, Monetary): Score customers on how recently and frequently
they purchased, and how much they spent, to identify high-value customers.
27. Predictive analytics: Build models to predict which customers are likely to respond to a campaign
or churn.
28. A/B Testing: Statistically compare two campaign variants to determine which performs better.
Illustrative R workflow
library(dplyr)
# Aggregate campaign performance by channel
campaign_summary <- campaign_data %>%
group_by(channel) %>%
summarise(
total_spend = sum(spend),
total_conversions = sum(conversions),
conversion_rate = mean(conversions / impressions)
) %>%
arrange(desc(conversion_rate))
print(campaign_summary)
Business Value
• Optimized marketing budget allocation across channels.
• Personalized targeting leading to higher conversion rates.
20 of 25
Data Analytics with R — Course Notes
• Reduced customer acquisition cost and improved retention.
5.3 Case Study: Logistics Management Analytics
Business Problem
Logistics and supply-chain operations need to minimize delivery times and costs while maintaining service
quality, requiring optimization of routes, inventory, and warehouse operations.
Typical Data Sources
• Shipment and delivery records (origin, destination, time, cost).
• Vehicle/fleet telemetry (GPS location, fuel consumption).
• Warehouse inventory levels.
• Historical demand data.
Analytical Approach
29. Descriptive analytics: Track KPIs such as average delivery time, on-time delivery percentage, and
cost per shipment.
30. Route optimization: Use optimization algorithms to minimize distance/time/fuel cost across
delivery routes.
31. Demand forecasting: Predict future shipment volumes to plan fleet and warehouse capacity.
32. Inventory analysis: Identify slow-moving vs fast-moving stock using summary statistics to optimize
stock levels.
33. Anomaly/outlier detection: Flag unusually delayed shipments or cost spikes using IQR-based outlier
detection (as covered in Unit IV).
Illustrative R workflow
library(dplyr)
# Identify delivery-time outliers using IQR method
Q1 <- quantile(shipment_data$delivery_time, 0.25)
Q3 <- quantile(shipment_data$delivery_time, 0.75)
iqr_val <- IQR(shipment_data$delivery_time)
delayed_outliers <- shipment_data %>%
filter(delivery_time > (Q3 + 1.5 * iqr_val))
print(delayed_outliers)
Business Value
• Reduced transportation and warehousing costs.
• Improved on-time delivery performance and customer satisfaction.
• Better demand-driven inventory planning, reducing stockouts and overstock.
5.4 Case Study: Insurance Analytics
Business Problem
Insurance companies must accurately assess risk to price policies fairly, detect fraudulent claims, and
predict customer churn — all while remaining profitable.
Typical Data Sources
• Policyholder demographic and history data.
21 of 25
Data Analytics with R — Course Notes
• Claims data (type, amount, date, approval status).
• External risk factors (location, vehicle type, health records for relevant insurance types).
Analytical Approach
34. Risk scoring/underwriting: Use historical data and statistical models to assess the risk profile of a
new policyholder and set premiums.
35. Fraud detection: Apply anomaly detection techniques to flag claims that deviate significantly from
normal patterns (e.g., using IQR or clustering-based outlier detection).
36. Churn prediction: Model the likelihood a policyholder will not renew, to enable proactive retention
offers.
37. Claims summarization: Use descriptive statistics (mean claim amount, variance by category) to
monitor claims trends over time.
Illustrative R workflow
library(dplyr)
# Summarize claims by policy type
claims_summary <- claims_data %>%
group_by(policy_type) %>%
summarise(
avg_claim = mean(claim_amount, [Link] = TRUE),
sd_claim = sd(claim_amount, [Link] = TRUE),
total_claims = n()
)
# Flag potentially fraudulent high-value claims (outliers)
high_value_flags <- claims_data %>%
filter(claim_amount > (mean(claim_amount, [Link] = TRUE) +
3 * sd(claim_amount, [Link] = TRUE)))
Business Value
• More accurate, fair premium pricing based on risk.
• Reduced losses from fraudulent claims.
• Improved customer retention through early churn intervention.
5.5 Case Study: Behavioural Analytics
Business Problem
Organizations (e-commerce platforms, apps, streaming services) want to understand how users interact
with their product to improve engagement, retention, and conversion.
Typical Data Sources
• Clickstream data (page visits, click sequences, time spent).
• App usage logs (session frequency, feature usage).
• Funnel data (steps from sign-up to purchase).
Analytical Approach
38. Funnel analysis: Track drop-off rates at each stage of a user journey (e.g., visit → add-to-cart →
checkout → purchase).
39. Cohort analysis: Group users by shared characteristics (e.g., sign-up month) and compare their
behaviour/retention over time.
22 of 25
Data Analytics with R — Course Notes
40. Engagement scoring: Combine multiple behavioural metrics (frequency, recency, session length)
into a single engagement score.
41. Predictive modelling: Predict user churn or likelihood of conversion based on behavioural patterns.
Illustrative R workflow
library(dplyr)
# Funnel drop-off analysis
funnel_summary <- user_events %>%
group_by(funnel_stage) %>%
summarise(users = n_distinct(user_id)) %>%
arrange(match(funnel_stage, c("visit", "add_to_cart", "checkout",
"purchase")))
funnel_summary <- funnel_summary %>%
mutate(drop_off_rate = 1 - users / lag(users))
Business Value
• Identification of friction points in the user journey to improve conversion.
• More effective, personalized engagement/retention campaigns.
• Data-driven product feature prioritization.
5.6 Case Study: Data Analytics on the Diamond Dataset
The 'diamonds' dataset (bundled with the ggplot2 package) is a classic, widely used dataset for teaching
data analytics in R. It contains prices and attributes of approximately 54,000 diamonds, making it well
suited for practicing the full analytics pipeline covered across this course.
Dataset attributes
Attribute Description
price Price of the diamond in US dollars
carat Weight of the diamond
cut Quality of the cut (Fair, Good, Very Good, Premium, Ideal)
color Diamond colour grade, from J (worst) to D (best)
clarity A measurement of how clear the diamond is (I1 worst to IF best)
depth Total depth percentage
table Width of the top of the diamond relative to widest point
x, y, z Length, width, and depth in millimetres
End-to-end analytics workflow demonstrated on this dataset
42. Import: Load the dataset directly from ggplot2, or from an exported CSV.
43. Pre-processing: Check for and handle missing/invalid values (e.g., diamonds with x, y, or z equal to
0, which are physically invalid).
44. Manipulation: Use dplyr's select(), filter(), mutate(), and arrange() to explore relationships, e.g.,
filtering diamonds above a certain carat, or creating a price-per-carat column.
45. Summarization: Compute mean, median, variance, standard deviation, and IQR of price grouped by
cut/color/clarity.
46. Outlier detection: Apply the IQR method to identify unusually priced diamonds for a given
carat/cut combination.
47. Visualization: Use histograms, boxplots, and scatter plots to explore price distribution and its
relationship with carat, cut, and clarity.
23 of 25
Data Analytics with R — Course Notes
library(ggplot2)
library(dplyr)
data(diamonds)
# Data pre-processing: remove invalid dimension records
clean_diamonds <- diamonds %>%
filter(x > 0, y > 0, z > 0)
# Data manipulation: create price-per-carat, filter, sort
top_value <- clean_diamonds %>%
mutate(price_per_carat = price / carat) %>%
filter(cut == "Ideal") %>%
arrange(desc(price_per_carat)) %>%
select(carat, cut, color, clarity, price, price_per_carat)
# Summarization: average price by cut
price_by_cut <- clean_diamonds %>%
group_by(cut) %>%
summarise(
avg_price = mean(price),
median_price = median(price),
sd_price = sd(price),
iqr_price = IQR(price)
)
# Outlier detection on price using IQR
Q1 <- quantile(clean_diamonds$price, 0.25)
Q3 <- quantile(clean_diamonds$price, 0.75)
iqr_val <- IQR(clean_diamonds$price)
outliers <- clean_diamonds %>%
filter(price > (Q3 + 1.5 * iqr_val))
# Visualization
ggplot(clean_diamonds, aes(x = cut, y = price)) +
geom_boxplot() +
theme_minimal() +
labs(title = "Diamond Price Distribution by Cut")
ggplot(clean_diamonds, aes(x = carat, y = price, color = cut)) +
geom_point(alpha = 0.3) +
theme_minimal() +
labs(title = "Price vs Carat, coloured by Cut")
Key insights typically derived
• Price shows a strong positive (though non-linear) relationship with carat weight.
• Counter-intuitively, 'Ideal' cut diamonds sometimes show a lower average price than 'Premium' or
'Fair' cuts in raw comparison, because cut quality is confounded with carat size — a reminder to
control for confounding variables in analysis.
• Price distributions are right-skewed with a number of high-value outliers, best summarized using
median/IQR rather than mean/SD alone.
Unit V — Summary
• Marketing analytics uses segmentation, RFM analysis, and predictive modelling to optimize
campaigns and targeting.
• Logistics analytics focuses on route optimization, demand forecasting, and delay/anomaly
detection.
24 of 25
Data Analytics with R — Course Notes
• Insurance analytics applies risk scoring, fraud detection, and churn prediction.
• Behavioural analytics uses funnel and cohort analysis to understand and improve user
engagement.
• The diamonds dataset case study ties together the full pipeline: import, pre-processing, dplyr-
based manipulation, statistical summarization, outlier detection, and visualization.
25 of 25