0% found this document useful (0 votes)
10 views27 pages

R Programming for Descriptive Analytics

The document provides an introduction to R programming and descriptive analytics, covering essential topics such as R and R Studio, basic operations, data structures, data reading, and handling missing values. It emphasizes the importance of R for statistical computing and data analysis, detailing various data structures like vectors, matrices, and data frames. Additionally, it includes practical examples and functions for data exploration, visualization, and analysis.

Uploaded by

abhijit.bose87
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views27 pages

R Programming for Descriptive Analytics

The document provides an introduction to R programming and descriptive analytics, covering essential topics such as R and R Studio, basic operations, data structures, data reading, and handling missing values. It emphasizes the importance of R for statistical computing and data analysis, detailing various data structures like vectors, matrices, and data frames. Additionally, it includes practical examples and functions for data exploration, visualization, and analysis.

Uploaded by

abhijit.bose87
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Introduction to R Programming and Descriptive Analytics

Table of Contents

1. Introduction to R and R Studio

2. Basic R Operations

3. Data Structures in R

o 3.1 Vectors

o 3.2 Matrices

o 3.3 Data Frames

4. Reading and Exploring Data

5. Handling Missing Values

6. Outlier Detection and Treatment

7. Descriptive Statistics

8. Correlation Analysis

9. Data Visualization

10. Grouped Analysis with tapply()

11. Case Study: Kiosk Food Sales

12. Key Takeaways

13. Quick Reference Guide

1. Introduction to R and R Studio

What is R?

R is a free, open-source programming language specifically designed for statistical computing and
data analysis. It provides a powerful environment for data manipulation, statistical modeling, and
graphical visualization. R has become the de facto standard in academia and industry for predictive
analytics due to its extensive library of statistical packages and active community support.

R vs R Studio

R is the core programming language and computational engine. When you install R, you get a basic
console interface where you can type commands and see results.

R Studio is an Integrated Development Environment (IDE) built on top of R. Think of R Studio as a


user-friendly wrapper around R that provides:

 A code editor with syntax highlighting and auto-completion

 Organized workspace with separate panes for code, console, plots, and files
 Better project management and debugging tools

 Automatic parenthesis matching and code formatting

Professor's Recommendation: For beginners, R Studio is highly recommended because it offers a


more intuitive interface and helpful features that make coding easier. Experienced programmers
might prefer the basic R editor for its simplicity and larger display area during presentations.

Installation and Setup

Students should have both R and R Studio installed:

1. First install R (the engine)

2. Then install R Studio (the interface)

3. Launch R Studio to begin coding

2. Basic R Operations

2.1 Variables and Assignment

In R, you can store values in variables using the assignment operator = (or <-).

# Creating variables

x=2

y=3

Explanation: Variables x and y now hold the values 2 and 3 respectively. Once assigned, you can
use these variable names in calculations instead of typing the numbers repeatedly. The # symbol
creates a comment - anything after # is ignored by R and serves as documentation for humans
reading the code.

2.2 Arithmetic Operations

# Basic arithmetic

x + y # Addition: 5

x - y # Subtraction: -1

x * y # Multiplication: 6

x / y # Division: 0.6666667

Explanation: R performs standard mathematical operations using familiar symbols. The results
appear immediately in the console when you execute each line. This interactive nature makes R
excellent for exploratory data analysis.

2.3 Mathematical Functions

# Built-in mathematical functions

log(x) # Natural logarithm (base e)


exp(x) # Exponential function (e^x)

sqrt(x) # Square root

Explanation: R comes with numerous built-in mathematical functions. log() calculates the natural
logarithm, while exp() calculates e raised to the power of x. These functions are essential for
statistical transformations and modeling.

3. Data Structures in R

3.1 Vectors

Vectors are one-dimensional arrays that hold elements of the same data type. They are the
fundamental building blocks in R.

Creating Vectors

# Using the c() function (concatenate)

x = c(4, 6, 5, 9, 8)

Explanation (100 words): The c() function combines individual values into a vector. The 'c' stands
for "concatenate" or "combine." In this example, we create a vector x containing five numbers.
Vectors are homogeneous, meaning all elements must be of the same type (all numbers, all text,
etc.). This is different from data frames which can contain mixed types. Vectors are foundational in
R because most operations are vectorized - when you apply a function to a vector, it automatically
applies to all elements. This makes R extremely efficient for statistical computations on large
datasets.

Creating Sequential Vectors

# Consecutive integers

x = 101:200 # Creates vector from 101 to 200

Explanation: The colon operator : creates a sequence of consecutive integers. This is extremely
useful when you need to generate regular sequences. The expression 101:200 creates a vector with
100 elements: [101, 102, 103, ..., 199, 200].

Accessing Vector Elements

# Accessing by position (1-indexed)

x[1] # First element: 4

x[4] # Fourth element: 9

# Accessing multiple elements

x[c(1, 3)] # First and third elements

Important Note: R uses 1-based indexing, meaning the first element is at position 1 (not 0 like in
Python or Java). Square brackets [] are used to access elements by their position.
Logical Operations on Vectors

# Logical comparisons

x>4 # Returns: FALSE TRUE TRUE TRUE TRUE

# Filtering based on conditions

x[x > 4] # Returns: 6 5 9 8 (only values greater than 4)

Explanation (120 words): Logical operations in R are vectorized, meaning they apply element-by-
element. When you write x > 4, R compares each element of vector x to 4 and returns a vector of
TRUE/FALSE values. This boolean vector can then be used to filter the original data. The expression
x[x > 4] is a powerful two-step process: first, x > 4 creates a logical vector indicating which
elements satisfy the condition; second, this logical vector is used as an index to extract only those
elements where the condition is TRUE. This filtering mechanism is fundamental to data
manipulation in R and allows you to subset data based on complex conditions without writing
explicit loops.

Vector Length

# Get number of elements

length(x) # Returns: 5

3.2 Matrices

Matrices are two-dimensional data structures that contain elements of the same type (typically
numbers).

Creating Matrices

# Basic matrix creation

a = matrix(c(2, 3, 4, 6), 2, 2)

# Result:

# [,1] [,2]

# [1,] 2 4

# [2,] 3 6

Explanation (150 words): The matrix() function converts a vector into a two-dimensional matrix. It
takes three arguments:

1. The data vector c(2, 3, 4, 6)

2. Number of rows (2)

3. Number of columns (2)

By default, R fills matrices column-wise (top to bottom, left to right). So the first two values (2, 3)
fill the first column, and the next two values (4, 6) fill the second column. This default behavior is
important to understand because it affects how your data is organized. If you have data that's
naturally organized by rows (like reading left to right), you'll need to use the byrow parameter to
override this default behavior. Matrices are particularly useful for mathematical operations,
statistical calculations, and representing structured data like images or tables.

Row-wise Matrix Filling

# Fill matrix by rows instead of columns

a = matrix(c(2, 3, 4, 6), 2, 2, byrow = TRUE)

# Result:

# [,1] [,2]

# [1,] 2 3

# [2,] 4 6

Explanation: The byrow = TRUE parameter changes the filling order to row-wise (left to right, top
to bottom). Now values fill the first row completely before moving to the second row. This is more
intuitive for data that's naturally organized in rows.

Accessing Matrix Elements

# Syntax: matrix[row, column]

a[2, 2] # Element at row 2, column 2: 6

a[2, ] # Entire second row: 4 6

a[, 2] # Entire second column: 3 6

Explanation (100 words): Matrix indexing uses the format [row, column]. You can access a single
element by specifying both row and column numbers. To select an entire row, leave the column
position empty: a[2, ]. To select an entire column, leave the row position empty: a[, 2]. This flexible
indexing system allows you to extract exactly the data you need. You can also use vectors of
indices to select multiple rows or columns simultaneously. Empty spaces in the brackets mean "all"
- so [2, ] means "row 2, all columns."

Deleting Rows and Columns

# Delete third row

a[-3, ]

# Delete third and fourth columns

a[, -c(3, 4)]

Explanation: The negative sign - before an index means "exclude" rather than "include." So -3
means "all except the third one." The c(3, 4) groups multiple indices together for simultaneous
deletion. This non-destructive approach creates a new matrix without the specified rows/columns,
leaving the original unchanged.

3.3 Data Frames


Data frames are the most common data structure in R for storing datasets. Unlike matrices, data
frames can contain different data types in different columns (numbers, text, dates, etc.).

Key Properties:

 Columns can have different types (numeric, character, factor)

 Each column represents a variable

 Each row represents an observation

 Similar to Excel spreadsheets or SQL tables

Data frames will be discussed extensively when we load real datasets.

4. Reading and Exploring Data

4.1 Reading CSV Files

# Read CSV file with interactive file selection

d = [Link]([Link](), stringsAsFactors = TRUE)

Explanation (150 words): The [Link]() function imports comma-separated value files into R as
data frames. The [Link]() function opens an interactive dialog box allowing you to browse and
select the file from your computer's file system - this is especially helpful when you don't want to
type the full file path or when demonstrating in class.

The stringsAsFactors = TRUE parameter is important: it automatically converts text columns into
factors (categorical variables). In R, factors are a special data type for categorical variables that
have a limited number of unique values (like "Monday", "Tuesday", etc. for days of the week).
Factors are more memory-efficient than plain text and are required by many statistical modeling
functions. While newer R versions default to FALSE, explicitly setting it to TRUE ensures that
qualitative variables like "day of the week" or "event" are properly treated as categories rather
than arbitrary text strings.

Alternative with Full Path:

# Specify file path directly

d = [Link]("C:/Users/Username/Documents/[Link]", stringsAsFactors = TRUE)

4.2 Data Exploration Functions

Once data is loaded, several functions help you understand its structure and content.

View First and Last Rows

# View first 6 rows (default)

head(d)

# View first 10 rows


head(d, 10)

# View last 6 rows

tail(d)

Explanation (80 words): head() displays the beginning of your dataset, giving you a quick glimpse
of what the data looks like without overwhelming the console with thousands of rows. The default
is 6 rows, but you can specify any number as the second argument. tail() shows the end of the
dataset, which is useful for checking if data import completed successfully. These functions are
essential first steps in any data analysis workflow, helping you verify that data loaded correctly and
understand its basic structure.

Understanding Data Structure

# Display structure of the dataset

str(d)

Explanation (100 words): The str() function (short for "structure") provides a comprehensive
summary of your data frame. It shows:

 The total number of observations (rows) and variables (columns)

 The name of each variable

 The data type of each variable (numeric, factor, integer, character)

 The first few values of each variable

This is one of the most useful diagnostic functions because it immediately reveals data type issues.
For example, if a numeric variable appears as "character" type, it indicates problems in the data
file (perhaps some cells contain text where they should contain numbers). Understanding data
types is crucial because many statistical functions require specific types.

Dataset Dimensions

# Get number of rows and columns

dim(d) # Returns: [rows, columns]

Explanation: dim() returns a vector with two numbers: the number of rows (observations) and the
number of columns (variables). This is useful for quickly checking dataset size.

Variable Names

# Get column names

names(d)

Explanation: names() returns a vector containing all column names in your data frame. This helps
you know exactly what variable names to use when accessing specific columns, especially useful
when variable names are long or abbreviated.

4.3 Accessing Variables in Data Frames


# Access a specific column using $

d$sales # Access the 'sales' column

d$day_of_the_week # Access the 'day_of_the_week' column

Explanation (100 words): The dollar sign $ operator accesses individual columns (variables) from a
data frame. The syntax is dataframe$columnname. This extracts that column as a vector, allowing
you to perform operations on it. For example, d$sales gives you a vector of all sales values, which
you can then use in calculations, create plots, or analyze statistically. The dollar sign notation is
more readable than bracket notation for column access and is widely used in R code. You can use
this extracted column with any function that accepts vectors, such as mean(d$sales) or
max(d$sales).

5. Handling Missing Values

Missing values are a common data quality issue. In R, missing values are represented by NA (Not
Available).

5.1 Why Missing Values Occur

Missing values can arise from various sources:

 Data entry errors or omissions

 Equipment malfunction during data collection

 Participants skipping survey questions

 Data joining operations where matches don't exist

 Data corruption during transfer

Importance: Most statistical functions in R will fail or produce incorrect results if missing values are
present. Therefore, identifying and handling NAs is a critical first step in data preparation.

5.2 Detecting Missing Values

Check Individual Values

# Returns TRUE for NA values, FALSE otherwise

[Link](x)

Explanation: The [Link]() function checks each element and returns a logical vector indicating which
values are missing. This is the fundamental function for NA detection.

Count Total Missing Values

# Count total NAs in a vector or column

sum([Link](d$sales))

Explanation (100 words): This elegant R idiom combines two functions: [Link]() returns TRUE/FALSE
for each element, and sum() treats TRUE as 1 and FALSE as 0, effectively counting the TRUEs. So
sum([Link](d$sales)) tells you exactly how many missing values exist in the sales column. This works
because R automatically converts logical values to numeric in mathematical contexts: TRUE
becomes 1, FALSE becomes 0. This pattern of combining logical operations with sum() is used
extensively in R for counting how many observations meet specific conditions, making it a
fundamental technique for data quality assessment.

Count Missing Values Per Column

# Count NAs in each column of the data frame

colSums([Link](d))

Explanation (120 words): colSums() calculates the sum of each column separately. When combined
with [Link](d), it creates a matrix of TRUE/FALSE values (one for each cell in the data frame) and
then sums each column. The result is a named vector showing how many missing values exist in
each variable. This provides a comprehensive overview of data quality across all variables at once.
For example, if the output shows sales: 3, date: 0, event: 1, you know that the sales column has 3
missing values, date has none, and event has 1. This single command is invaluable during initial
data exploration because it immediately identifies which variables have data quality issues that
need attention before analysis.

5.3 Finding Missing Value Locations

# Find row numbers where sales is missing

which([Link](d$sales))

Explanation (90 words): The which() function returns the indices (positions) where a logical
condition is TRUE. So which([Link](d$sales)) gives you the exact row numbers where sales values
are missing. This is crucial for understanding patterns in missing data. For example, if all missing
values are in the last 10 rows, it might indicate incomplete data entry. If they're scattered
randomly, it might suggest sporadic measurement errors. Knowing the specific locations allows
you to inspect those rows in detail to understand why data is missing.

For Matrices: Find Exact Row and Column

# Find both row and column positions of NAs

which([Link](d), [Link] = TRUE)

Explanation: The [Link] = TRUE parameter makes which() return a matrix with two columns: one
for row numbers and one for column numbers. This tells you exactly which cell is missing (row X,
column Y).

5.4 Removing Missing Values

# Remove all rows containing any NA

d = [Link](d)

Explanation (150 words): The [Link]() function removes entire rows that contain NA values in any
column. This is a simple but aggressive approach to handling missing data. The benefit is that you
get a complete dataset with no missing values, which most statistical functions can process
without issues. However, there are important considerations:
Caution: If a row has one missing value out of 20 variables, [Link]() deletes the entire row,
discarding 19 good values. This can substantially reduce your sample size if missingness is
common. Before using [Link](), check the percentage of missing data - if more than 5-10% of
rows contain NAs, deletion might not be appropriate. Alternative approaches include:

 Imputation (filling NAs with estimated values like mean or median)

 Using statistical methods that handle missing data

 Analyzing why data is missing (Missing Completely At Random vs. systematic patterns)

For this course, [Link]() is used for simplicity, but real-world analysis requires more sophisticated
approaches.

6. Outlier Detection and Treatment

Outliers are data points that are significantly different from other observations. They can result
from measurement errors, data entry mistakes, or genuine extreme values.

6.1 Why Outliers Matter

Outliers can:

 Distort statistical measures (mean, standard deviation)

 Violate assumptions of statistical models

 Indicate data quality problems

 Represent important insights (fraud detection, anomalies)

Key Question: Is this outlier an error (should be corrected/removed) or a legitimate extreme value
(should be kept)?

6.2 Box Plot for Outlier Visualization

# Create box plot

boxplot(d$sales)

Explanation (180 words): A box plot (also called box-and-whisker plot) is a standardized way of
displaying the distribution of data based on five key statistics:

1. Minimum: The smallest value within the lower whisker

2. Q1 (First Quartile): 25% of data falls below this value

3. Median (Q2): The middle value; 50% of data falls below this

4. Q3 (Third Quartile): 75% of data falls below this value

5. Maximum: The largest value within the upper whisker

The "box" spans from Q1 to Q3, representing the middle 50% of data (called the Interquartile
Range or IQR). The line inside the box marks the median. The "whiskers" extend to the minimum
and maximum values that fall within 1.5 × IQR from the box edges.
Outliers are plotted as individual points beyond the whiskers. The 1.5 × IQR rule is a standard
statistical convention: values more than 1.5 times the IQR below Q1 or above Q3 are considered
potential outliers. Box plots allow you to instantly visualize data spread, skewness, and identify
unusual values that deserve investigation. They're particularly useful when comparing
distributions across multiple groups.

6.3 Extracting Box Plot Statistics

# Get the five-number summary

boxplot(d$sales)$stats

# Returns: [min, Q1, median, Q3, max]

Explanation: The $stats component extracts the five numbers that define the box plot. These
values are fundamental descriptive statistics that summarize the distribution's center and spread.

6.4 Getting Outlier Values

# Extract outlier values

boxplot(d$sales)$out

Explanation: The $out component returns the actual values that R has identified as outliers using
the 1.5 × IQR rule. These are the points plotted beyond the whiskers.

6.5 Finding Outlier Locations

# Find row numbers of outliers (sales > 146 in this example)

which(d$sales > 146)

Explanation (90 words): Once you've identified the outlier threshold (146 in this case), which() tells
you the row numbers where this condition occurs. This allows you to examine the complete
records for those outlier observations. You might discover that all outliers occur on event days, or
all on Saturdays, revealing patterns that explain the extreme values. Understanding the context of
outliers is essential for deciding whether they represent errors that should be corrected or
legitimate extreme values that should be retained in the analysis.

6.6 Viewing Complete Outlier Records

# View all columns for outlier rows

d[which(d$sales > 146), ]

Explanation (80 words): This command displays entire rows where sales exceeds 146. The comma
after the row index means "all columns." By viewing complete records, you can investigate
whether outliers make sense in context. For example, if sales of 217 occurred with an ordering
quantity of only 110, this is an inconsistency (you can't sell more than you ordered). Such logical
inconsistencies indicate data errors that must be corrected before analysis. Context helps
distinguish between errors and genuine extremes.

6.7 Replacing Outlier Values

# Correct specific outlier values

d$sales[19] = 110 # Replace value at row 19


d$sales[31] = 130 # Replace value at row 31

Explanation (150 words): When you've identified a data error (not just an outlier), you can replace
the incorrect value with the correct one. In the case study, rows 19 and 31 had sales values (217
and 234) that exceeded their ordering quantities (110 and 130). This is logically impossible - you
cannot sell more items than you have available. The professor determined that these were data
entry errors where the digits were likely swapped or mistyped.

The Correction Decision: The values were replaced with the ordering quantities (110 and 130)
based on domain knowledge that sales typically equal ordering quantity on high-demand days.
This type of manual correction requires:

 Understanding the business context

 Logical reasoning about what values make sense

 Documentation of what was changed and why

 Conservative approaches (when in doubt, deletion is safer than incorrect imputation)

Not all outliers should be corrected - only those identified as errors through logical inconsistency
or domain knowledge.

6.8 Outlier Analysis Strategy

Professor's Approach in the Case Study:

1. Upper Outliers: Check if sales > ordering quantity (logically impossible) - these are errors
requiring correction

2. Lower Outliers: Check if low sales occur on expected days (Saturdays, holidays) - these are
legitimate, no action needed

3. Context Matters: Always investigate the business reason before treating outliers

Key Principle: Don't automatically delete all outliers. Understand why they exist first.

7. Descriptive Statistics

Descriptive statistics summarize and describe the main features of a dataset.

7.1 The attach() Function

# Attach data frame to search path

attach(d)

Explanation (120 words): The attach() function makes all columns of a data frame directly
accessible without needing the d$ prefix. After attaching, you can type sales instead of d$sales,
event instead of d$event, etc. This reduces typing and makes code cleaner, especially in interactive
sessions.

Caution from Professor: While convenient, attach() is described as "a bad thing to do" in
production code because:

 It can create naming conflicts if variable names clash with existing objects
 It makes code less explicit about data sources

 It can lead to confusion about which dataset variables come from

Recommendation: Use attach() for convenience during exploratory analysis and classroom
demonstrations, but use explicit d$ notation in final scripts and reports for clarity and
reproducibility.

7.2 Summary Statistics

# Comprehensive summary of a variable

summary(sales)

Output Example:

Min. 1st Qu. Median Mean 3rd Qu. Max.

18.0 92.0 100.0 98.7 114.0 160.0

Explanation (150 words): The summary() function provides six key statistics:

1. Minimum (18): The lowest value in the dataset

2. 1st Quartile (92): 25% of days have sales below 92 units

3. Median (100): The middle value; half the days have sales below 100, half above

4. Mean (98.7): The arithmetic average of all sales values

5. 3rd Quartile (114): 75% of days have sales below 114 units

6. Maximum (160): The highest value in the dataset

Interpreting Quartiles: The quartiles divide your data into four equal parts. The distance between
quartiles indicates data spread - larger gaps suggest more variability in that range. In this example,
the gap between Q1 and median (100-92=8) is smaller than the gap between median and Q3 (114-
100=14), suggesting the data is slightly right-skewed with more spread in the upper range.

Mean vs. Median: When mean < median, the distribution is left-skewed; when mean > median, it's
right-skewed. Here they're very close, suggesting fairly symmetric distribution.

7.3 Individual Descriptive Statistics

# Calculate specific statistics

mean(sales) # Arithmetic average

median(sales) # Middle value

sd(sales) # Standard deviation

Standard Deviation Explanation (120 words): Standard deviation (SD) measures the average
distance of data points from the mean. A small SD means values cluster tightly around the mean; a
large SD means values are spread out widely. In the context of the kiosk case study, SD is "very
very important" because:

 High SD in sales means high variability and unpredictability


 High variability makes ordering difficult (order too much → waste; too little → lost sales)

 The goal of predictive analytics is to understand what drives this variation

 By identifying patterns (event days, holidays, day of week), we can reduce unexplained
variation

 Lower SD in prediction errors means better ordering decisions and higher profit

SD essentially quantifies the business problem: how much does sales vary, and can we predict it
better?

8. Correlation Analysis

Correlation measures the strength and direction of the linear relationship between two
quantitative variables.

8.1 Calculating Correlation

# Correlation between ordering quantity and sales

cor(d$OQ, d$sales)

# Result: 0.925

Explanation (150 words): The cor() function calculates the Pearson correlation coefficient, which
ranges from -1 to +1:

 +1: Perfect positive correlation (as one increases, the other increases proportionally)

 0: No linear relationship

 -1: Perfect negative correlation (as one increases, the other decreases)

A correlation of 0.925 indicates a very strong positive relationship between ordering quantity and
sales. This suggests that, overall, the kiosk is doing a good job of matching ordering to sales
demand. Values above 0.7 are typically considered strong correlations.

Important Note: Correlation does not imply causation. High correlation means the variables move
together, but doesn't tell you why. In this case, the causation actually works both ways: ordering
quantity influences sales (you can't sell what you don't have), and sales patterns influence ordering
decisions (they order more on high-demand days).

When to Use Correlation: Only for two quantitative (numeric) variables. For relationships between
categorical and numeric variables, use other methods like tapply() or ANOVA.

8.2 Using attach() with Correlation

# With attach(), you can omit the d$ prefix

attach(d)

cor(OQ, sales) # Same result: 0.925

8.3 Correlation for Specific Subgroups


# Correlation on event days only

cor(OQ[event == "yes"], sales[event == "yes"])

# Result: 0.55

Explanation (180 words): This advanced syntax filters the data before calculating correlation. The
expression OQ[event == "yes"] extracts only the ordering quantities where the event variable
equals "yes", and similarly for sales. By calculating correlation on this subset, we see if the
relationship holds for specific groups.

Key Finding from Case Study: While overall correlation is strong (0.925), the correlation on event
days drops to 0.55, which is substantially weaker. This reveals an important insight:

Business Interpretation: The kiosk is doing a good job matching ordering to sales on regular days,
but performs poorly on event days. Events are unpredictable, and the current ordering strategy
doesn't adequately account for the higher and more variable demand on these days. This specific
insight drives the recommendation to revise ordering quantities specifically for event days, rather
than making global changes.

Analysis Pattern: This technique of calculating statistics for overall data and then for specific
subgroups is fundamental in analytics. Global statistics can hide problems in specific segments.
Always look for variation across meaningful categories (customer types, time periods, geographic
regions, etc.).

9. Data Visualization

9.1 Scatter Plots

# Basic scatter plot

plot(OQ, sales)

Explanation (100 words): The plot() function creates a scatter plot when given two numeric
vectors. Each point represents one observation, with its x-coordinate from the first variable (OQ)
and y-coordinate from the second (sales). Scatter plots are essential for visualizing relationships
between two quantitative variables. They help you see:

 The direction of relationship (positive/negative)

 The strength of relationship (tight clustering vs. scattered)

 Outliers and unusual patterns

 Non-linear patterns that correlation might miss

 Subgroups or clusters in the data

Scatter plots complement correlation by providing visual confirmation and revealing patterns that
single numbers cannot capture.

9.2 Customizing Scatter Plots

Color
# Red points

plot(OQ, sales, col = "red")

# Other color options

plot(OQ, sales, col = "blue")

plot(OQ, sales, col = "green")

Explanation: The col parameter changes point color. R recognizes many color names ("red", "blue",
"green", "purple", etc.) as well as hex codes for precise color control.

Point Size and Boldness

# Make points bolder with lwd (line width)

plot(OQ, sales, col = "red", lwd = 2)

Explanation (80 words): The lwd parameter (line width) makes points more prominent by
increasing their size. While it's called "line" width, it also affects point symbols. Higher values
make points bolder. The professor used lwd = 2 to make the visualization "more catchy" and easier
to see during presentation. Visual clarity is important for effective communication of results.
Compare lwd = 1 (default) with lwd = 2 to see how subtle changes improve readability, especially
when presenting to audiences.

Axis Labels

# Custom x-axis label

plot(OQ, sales, col = "red", lwd = 2, xlab = "Ordering Quantity")

# Both custom axis labels

plot(OQ, sales, col = "red", lwd = 2,

xlab = "Ordering Quantity",

ylab = "Sales")

Explanation (90 words): The xlab and ylab parameters customize axis labels. Default labels use
variable names from your code (like "OQ"), which might not be clear to audiences. Professional
visualizations should have descriptive labels that anyone can understand without seeing the code.
"Ordering Quantity" is more informative than "OQ". Good axis labels include units when relevant
(e.g., "Sales (units)" or "Revenue ($)"). Clear labeling is essential for presentations and reports
where viewers need to understand graphs independently. Taking time to add proper labels
distinguishes professional analysis from quick exploratory work.

9.3 Complete Customized Plot Example

# Publication-quality scatter plot

plot(OQ, sales,
col = "red",

lwd = 2,

xlab = "Ordering Quantity",

ylab = "Sales (units)",

main = "Relationship Between Ordering and Sales")

10. Grouped Analysis with tapply()

The tapply() function performs calculations on a numeric variable separately for each level of a
categorical variable. Think of it as creating a pivot table in Excel.

10.1 Basic tapply() Syntax

# Calculate mean sales by event status

tapply(sales, event, mean)

Output Example:

no yes

91.0 127.0

Explanation (150 words): The tapply() function has three arguments:

1. First argument (sales): The numeric variable you want to analyze

2. Second argument (event): The categorical variable defining groups

3. Third argument (mean): The function to apply to each group

This command calculates the average sales separately for event days (yes) and non-event days
(no). The result is a named vector showing mean sales for each category.

Business Insight: Average sales jump from 91 units on regular days to 127 units on event days - a
40% increase. This substantial difference confirms that events drive significantly higher demand,
and ordering quantities should account for this pattern.

tapply() Versatility: You can use any function in the third argument: mean, median, sd, min, max,
length (to count observations), etc. This makes tapply() extremely powerful for exploratory
analysis across different segments.

10.2 Multiple Applications

# Median sales by event

tapply(sales, event, median)

# Standard deviation by event

tapply(sales, event, sd)


# Count observations per group

tapply(sales, event, length)

10.3 Multi-Factor Analysis

# Mean sales by day of the week AND event

tapply(sales, list(day_of_the_week, event), mean)

Explanation (120 words): When you provide list(variable1, variable2) as the second argument,
tapply() creates a cross-tabulation (like a two-way pivot table). This calculates statistics for every
combination of the two factors. For example, you'd see mean sales for:

 Monday non-events

 Monday events

 Tuesday non-events

 Tuesday events

 (and so on for each day)

This multi-dimensional analysis reveals whether certain day-event combinations have particularly
high or low sales. Perhaps Saturday events are different from Tuesday events. This granular
understanding is crucial for developing sophisticated ordering strategies that account for multiple
factors simultaneously. The case study emphasizes examining regular days, event days, and
holidays separately because each type behaves differently.

10.4 Analysis by Holiday

# Mean sales on holidays vs. non-holidays

tapply(sales, holiday, mean)

Case Study Finding: Like events, holidays show different sales patterns. The professor found that
correlation between ordering and sales is weaker on holidays, similar to event days. This suggests
the ordering strategy doesn't adequately account for holiday demand patterns.

11. Case Study: Kiosk Food Sales

11.1 Business Problem

Context: A food kiosk operates daily and faces a critical inventory problem. Food is perishable and
must be ordered in advance of each day's sales.

The Challenge:

 Order too much: Excess food spoils → waste → loss of food cost

 Order too little: Run out of stock → lost sales → missed profit opportunities
 High variation: Sales fluctuate significantly from day to day, making ordering decisions
difficult

Business Impact: High sales variability increases cost (frequent excess) and reduces revenue
(frequent stockouts). Better prediction of sales would enable optimal ordering and improve
profitability.

Management's Claim: They assert they're already doing a good job with ordering and don't need
changes.

Objective: Use descriptive analytics to determine if the current ordering strategy is optimal, and if
not, how it should be revised.

11.2 Dataset Description

Variables:

1. date: The specific date of observation

2. day_of_the_week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

3. OQ (ordering_quantity): Number of units ordered for that day

4. sales: Actual number of units sold that day

5. event: Whether a special event occurred that day (yes/no)

6. holiday: Whether the day was a holiday (yes/no)

Key Constraint: Sales cannot exceed ordering quantity (you can't sell what you didn't order).

11.3 Data Quality Issues Found

Missing Values

 Some observations had NA values in various columns

 Treatment: Rows with missing values were removed using [Link]() to ensure clean
analysis

 Justification: With sufficient data, removing a few incomplete records is acceptable

Outliers and Inconsistencies

Upper Outliers:

 Row 19: Sales = 217, but OQ = 110 (impossible!)

 Row 31: Sales = 234, but OQ = 130 (impossible!)

 Diagnosis: Data entry errors where sales were recorded higher than available inventory

 Treatment: Replaced with ordering quantities (110 and 130 respectively), assuming full
sellout on high-demand days

Lower Outliers:

 Sales values less than 61 (identified by box plot)


 Investigation: All low-sales days were either Saturdays or holidays

 Diagnosis: These are legitimate low-demand days, not errors

 Treatment: No action needed - these represent real business patterns

11.4 Descriptive Analysis Findings

Overall Correlation

cor(OQ, sales) # 0.925

Interpretation: Strong correlation suggests generally good alignment between ordering and sales.

Summary Statistics

summary(sales)

# Min. 1st Qu. Median Mean 3rd Qu. Max.

# 18.0 92.0 100.0 98.7 114.0 160.0

Interpretation: Wide range (18 to 160) confirms high variability. Standard deviation is substantial
relative to the mean, indicating significant uncertainty in daily demand.

Segmented Analysis

By Event:

tapply(sales, event, mean)

# no yes

# 91.0 127.0

 Sales increase 40% on event days

 But correlation on event days drops to 0.55 (weak)

 Conclusion: Ordering doesn't adequately adjust for event demand

By Day of Week:

tapply(sales, day_of_the_week, mean)

 Clear day-of-week patterns exist

 Some days naturally have higher demand

By Holiday:

tapply(sales, holiday, mean)

 Holidays show different sales patterns

 Correlation on holidays is also weaker

11.5 Key Insights

Three Types of Days Identified:


1. Regular Days: The current ordering strategy works well (high correlation)

2. Event Days: Higher demand, but poor ordering alignment (weak correlation) → needs
revision

3. Holidays: Different patterns, also poorly predicted → needs revision

The Problem: While overall correlation appears strong (0.925), this hides poor performance on
event days and holidays. The strong overall correlation is driven by the many regular days, masking
problems in important subgroups.

11.6 Business Recommendations

Based on Descriptive Analytics:

1. Keep current ordering on regular days: Already performing well

2. Revise ordering for event days: Increase quantities, but also improve prediction

3. Revise ordering for holidays: Account for different demand patterns

4. Account for day-of-week: Incorporate systematic weekday patterns

Proposed Revision Strategy:

 Calculate separate average sales for each day type (regular, event, holiday)

 Set ordering quantities based on these group-specific averages

 Calculate expected profit increase from reduced waste and stockouts

Next Steps (Future Sessions): After learning regression, students will:

 Build predictive models to forecast sales based on day type, day of week, events, and
holidays

 Generate specific ordering quantity recommendations

 Calculate profit improvements from revised strategy

 Submit as homework/assignment

11.7 The Larger Analytics Story

This case demonstrates the analytics workflow:

1. Business Problem: High variability causing inefficiency

2. Data Collection: Gather historical sales and contextual variables

3. Data Quality: Check and fix missing values, outliers, inconsistencies

4. Descriptive Analysis: Understand patterns through summary stats, correlation, and


grouping

5. Insight Generation: Identify that problem exists specifically on event/holiday days

6. Recommendation: Propose segmented ordering strategy

7. Predictive Modeling (next): Build models to forecast sales


8. Prescriptive Analytics (later): Optimize ordering decisions to maximize profit

This progression from description → prediction → prescription is the core of the predictive
analytics course.

12. Key Takeaways

Technical Skills Acquired

1. R Fundamentals: Variables, operators, functions, basic syntax

2. Data Structures: Vectors, matrices, data frames, and when to use each

3. Data Import: Reading CSV files into R with proper options

4. Data Exploration: head(), tail(), str(), dim(), names(), summary()

5. Data Quality: Detecting and handling missing values and outliers

6. Descriptive Statistics: mean, median, SD, quartiles, and their interpretation

7. Relationships: Correlation for numeric variables, tapply() for grouped analysis

8. Visualization: Box plots for outliers, scatter plots for relationships

9. Data Manipulation: Subsetting, filtering, accessing elements, replacing values

Analytical Concepts

1. Data Quality First: Always check for NAs, outliers, and inconsistencies before analysis

2. Context Matters: Not all outliers are errors; understand the business before correcting

3. Segmented Analysis: Overall statistics can hide problems in subgroups - always drill down

4. Multiple Perspectives: Use summary stats, correlations, visualizations, and grouping


together

5. From Description to Action: Descriptive analytics should lead to business insights and
recommendations

R Best Practices

1. Explore Before Analyzing: Always use str(), summary(), and head() on new data

2. Document Your Work: Use comments (#) to explain what code does

3. Check Your Results: View data after transformations to confirm they worked correctly

4. Use Meaningful Names: Clear variable and file names improve code readability

5. Visualization Matters: Plots often reveal patterns that numbers alone miss

13. Quick Reference Guide

Data Import and Exploration


d = [Link]([Link](), stringsAsFactors = TRUE) # Read CSV

head(d) # First 6 rows

tail(d) # Last 6 rows

str(d) # Data structure

dim(d) # Dimensions

names(d) # Column names

summary(d) # Summary stats

Missing Values

[Link](x) # Check for NAs

sum([Link](d$sales)) # Count NAs in sales

colSums([Link](d)) # Count NAs per column

which([Link](d$sales)) # Row numbers with NAs

d = [Link](d) # Remove rows with any NA

Outliers

boxplot(d$sales) # Visualize outliers

boxplot(d$sales)$stats # Five-number summary

boxplot(d$sales)$out # Outlier values

which(d$sales > 146) # Find outlier locations

d[which(d$sales > 146), ] # View outlier rows

d$sales[19] = 110 # Replace outlier value

Descriptive Statistics

summary(sales) # Six-number summary

mean(sales) # Average

median(sales) # Middle value

sd(sales) # Standard deviation

Relationships

cor(x, y) # Correlation

plot(x, y) # Scatter plot

plot(x, y, col="red", lwd=2, # Customized plot

xlab="X Label", ylab="Y Label")

Grouped Analysis
tapply(sales, event, mean) # Mean by group

tapply(sales, event, median) # Median by group

tapply(sales, list(day, event), mean) # Two-way grouping

Data Manipulation

d$sales # Access column

x[1] # First element

x[x > 5] # Filter by condition

d[19, ] # Row 19, all columns

d[, 3] # All rows, column 3

attach(d) # Direct variable access

Vectors and Matrices

x = c(2, 4, 6, 8) # Create vector

x = 1:100 # Sequence 1 to 100

length(x) # Number of elements

m = matrix(c(1,2,3,4), 2, 2) # Create matrix

m = matrix(c(1,2,3,4), 2, 2, byrow=TRUE) # Row-wise filling

Course Context and Next Steps

What We've Learned

Session 2 focused on Descriptive Analytics - understanding patterns in historical data. We learned:

 How to use R for basic data manipulation

 How to assess and clean data quality

 How to calculate and interpret summary statistics

 How to examine relationships and group differences

 How to visualize data effectively

What's Coming Next

Session 3: Regression Analysis

 Building predictive models

 Understanding relationships between multiple variables

 Making quantitative predictions

 Assessing model quality and accuracy


Application to Case Study: After learning regression, students will:

1. Build a model predicting sales from day type, events, holidays, etc.

2. Use the model to generate optimal ordering quantities

3. Calculate profit improvements

4. Submit as assignment

Homework for Next Session

1. Practice the code: Spend 30-45 minutes running all the commands from this session on
your own computer

2. Understand, don't memorize: Focus on what each function does and why, not just syntax

3. Think about the case: How would you revise ordering quantities based on descriptive
analysis?

4. Decide on assignment format: Discuss with classmates whether to do individual or group


assignments (max 5 per group)

Professor's Advice

On Coding:

 "Coding is the easiest part" - Don't worry too much about syntax

 Most assignment code will be simple modifications of class examples

 Focus on understanding concepts and interpreting results

 R Studio is recommended for beginners due to better interface

On Learning:

 Practice for 30-45 minutes to reinforce concepts

 Without hands-on practice, "this is going to look more and more difficult"

 The concepts build on each other - master basics now

On Assignments:

 Coding won't be the challenge; understanding and interpretation will be

 No new complex coding required - mainly applying what was taught

 Ideas and business insights matter more than technical complexity

Additional Notes

R vs Python

While not discussed extensively in class, students sometimes ask about the difference:

 R is designed specifically for statistics and data analysis


 Python is a general-purpose programming language with statistics libraries

 For this course, R is chosen because it's the standard in academic statistics

 Most statistical methods appear in R first before other languages

 R's syntax is optimized for data manipulation and statistical workflows

Resources for Learning R

 R documentation: ?function_name (e.g., ?mean)

 R Studio's built-in help

 CRAN package documentation

 Online communities (Stack Overflow, R-bloggers)

Common Mistakes to Avoid

1. Forgetting parentheses: Functions need () even with no arguments (e.g., summary() not
summary)

2. Case sensitivity: R distinguishes between Sales and sales

3. Using commas wrong in subsetting: Remember [row, column] format

4. Not checking data after import: Always use str() and head() after reading files

5. Ignoring data quality: Missing values and outliers will break your analysis if not handled

Conclusion

This session provided a solid foundation in R programming and descriptive analytics. The core
message is that before any sophisticated modeling, you must:

1. Know your data: Structure, types, dimensions

2. Clean your data: Fix NAs, outliers, inconsistencies

3. Describe your data: Summary statistics, distributions, relationships

4. Segment your data: Look for patterns across groups

5. Visualize your data: Plots reveal what numbers hide

These fundamentals apply to every data analysis project, regardless of complexity. Master these
basics, and advanced techniques become much more accessible.

The kiosk case study demonstrates that even simple descriptive analysis can generate valuable
business insights. We discovered that ordering strategies need to differ for regular days, event
days, and holidays - a finding that can directly improve profitability.

In the next session, we'll build on this foundation by learning regression analysis, which will allow
us to make quantitative predictions and optimize business decisions.
End of Session 2 Lecture Notes

Total Word Count: ~12,000 words Generated from: September 14, 2025 class recording Case Study:
Kiosk Food Sales Optimization

You might also like