R LANGUAGE
BASIC :
1. ASSIGN 2 VALUES USING <- OPERATOR AND PRINT SUM OF TWO VALUES IN R LANGUAGE -
CLICK CLICK2. ASSIGN 2 VALUES USING -> OPERATOR AND PRINT SUM USING CAT() IN R LANGUAGE -
CLICK CLICK3. ASSIGN 2 VALUES USING = OPERATOR AND PRINT SUM OF TWO VALUES IN R LANGUAGE -
CLICK CLICK4. ASSIGN 2 VALUES USING -> OPERATOR AND PRINT SUM OF TWO VALUES IN R LANGUAGE -
CLICK CLICK5. ARITHMETIC OPERATOR IN R LANGUAGE ( USING TWO INTEGER VALUES ) -
CLICK CLICK 6. ARITHMETIC OPERATOR IN R LANGUAGE ( USING TWO FLOAT VALUES ) -
CLICK CLICK7. RELATIONAL OPERATORS IN R LANGUAGE -
CLICK CLICK10. NESTED IF STRUCTURE IN R LANGUAGE -
CLICK11. INPUT STRING FROM USER AND PRINT IN R LANGUAGE -
CLICK12. FOR LOOP STRUCTURE IN R LANGUAGE -
CLICK13. WHILE LOOP STRUCTURE IN R LANGUAGE -
CLICK14. SWITCH CASE IN R LANGAUAGE -
CLICK15. SWITCH CASE WHEN CASE VALUE GIVEN BY USER IN R LANGUAGE -
CLICK
VECTOR:
1. VECTOR CREATION, MODIFICATION,DELETION,BASIC OPERARTION OF TWO VECTORS IN R LANGUAGE --
CLICK2. SUM AND AVERAGE OF ELEMENTS INVECTOR IN R LANGUAGE -
CLICK3. LARGEST AND SMALLEST ELEMENT IN VECTOR IN R LANGUAGE -
CLICK4. LINEAR SEARCH IN R LANGUAGE -
CLICK5. BINARY SEARCH IN R LANGUAGE -
CLICK 6. BUBBLE SORT IN R LANGUAGE -
CLICK7. INSERTION SORT IN R LANGUAGE -
CLICK8. SELECTION SORT IN R LANGUAGE -
CLICK 9. RANDOMIZED QUICK SORT IN R LANGUAGE -
CLICK10. MERGE SORT IN R LANGUAGE -
CLICK11. VECTOR USING RANDOM VALUES -
CLICK
MATRIX :
1. MATRIX CREATION IN R LANGUAGE , ADDITION OF TWO MATRICES IN R LANGUAGE , SUBTRACTION OF TWO MATRICES IN R LANGUAGE, MULTIPLICATION OF TWO MATRICES IN R LANGUAGE, DIVISION OF TWO MATRICES IN R LANGUAGE -
CLICK
APPLY FAMILY IN R LANGUAGE:
1.APPLY() IN R LANGUAGE:
CLICK2.LAPPLY() IN R LANGUAGE:
CLICK3.SAPPLY () IN R LANGUAGE:
CLICK
DATA HANDLING :
2. Read data from CSV files - FIND LARGEST AND SMALLEST I)
CLICK II)
CLICK4.
DATA / SUBSET FROM CSV FILE IN R LANGUAGE CLICK5. Read data from txt file
CLICK
DATA PROCESSING / CLEANING
1. Type conversion in R language
CLICK
📘 Module 1: Introduction to R Programming
(6 Classes)
🎯 Learning Outcomes
After completing this module, students will be able to:
✔ Install R and RStudio
✔ Understand the RStudio Interface
✔ Write basic R programs
✔ Perform arithmetic and logical operations
✔ Work with different data types
✔ Create and manipulate vectors, lists, matrices and data frames
✔ Understand factors and categorical variables
CLASS 1
Introduction to R
What is R?
R is an open-source programming language specially designed for
-
Data Analysis
-
Statistics
-
Machine Learning
-
Artificial Intelligence
-
Data Visualization
-
Research
It was developed by
-
Ross Ihaka
-
Robert Gentleman
at the University of Auckland.
Today R is maintained by the R Foundation.
Why Learn R?
Advantages
✔ Free
✔ Open Source
✔ Easy to Learn
✔ Powerful Graphics
✔ Huge Package Library
✔ Excellent Statistical Functions
✔ Cross Platform
Applications of R
R is widely used in
-
Data Science
-
Business Analytics
-
Bioinformatics
-
Finance
-
Healthcare
-
Marketing
-
Machine Learning
-
Research
Installing R
Step 1
Download R from
https://cran.r-project.org
Install normally.
Step 2
Download RStudio
https://posit.co/download/rstudio-desktop/
Install after installing R.
CLASS 2
RStudio Interface
When RStudio opens, four main windows appear.
1. Source
Used to
-
Write scripts
-
Save programs
-
Edit code
Shortcut
2. Console
Used to execute commands immediately.
Example
Output
3. Environment
Shows
4. Files
Displays project files.
5. Plots
Displays graphs.
6. Packages
Shows installed packages.
7. Help
Displays documentation.
Example
Understanding the R Command Prompt
Console Prompt
means R is ready.
Example
CLASS 3
Basic Operations
Arithmetic Operators
| Operator | Meaning |
|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| ^ | Power |
| %% | Modulus |
| %/% | Integer Division |
Example
Output
Comparison Operators
| Operator | Meaning |
|---|
| > | Greater |
| < | Less |
| >= | Greater Equal |
| <= | Less Equal |
| == | Equal |
| != | Not Equal |
Example
Output
Logical Operators
| Operator | Meaning |
|---|
| & | AND |
| | | OR |
| ! | NOT |
Example
Output
CLASS 4
Data Types
R supports many data types.
Numeric
Output
Integer
Output
Character
Output
Logical
Output
Factor
Output
Variable Assignment
There are three assignment operators.
Output
Variable Naming Rules
✔ Can contain letters
✔ Numbers
✔ Underscore
✔ Dot
Cannot start with numbers.
Correct
Wrong
CLASS 5
Data Structures in R
Vector
A vector stores similar data.
Create Vector
Output
Length
Output
Class
Output
Type
Output
Indexing
Output
Multiple Values
Output
Functions
Output
List
Lists store different data types.
Output
Access
Output
Matrix
Stores data in rows and columns.
Output
Indexing
Output
Matrix Addition
Output
CLASS 6
Data Frame and Factors
Data Frame
Most important data structure.
Output
Structure
Summary
Access Column
First Row
Import CSV
Export CSV
Factors
Factors store categorical data.
Example
Output
Levels
Output
Frequency
Output
Summary of Data Structures
| Data Structure | Stores |
|---|
| Vector | Same Data Type |
| List | Different Data Types |
| Matrix | 2D Same Data Type |
| Data Frame | Tabular Data |
| Factor | Categorical Data |
Common Built-in Functions
| Function | Purpose |
|---|
| length() | Number of elements |
| class() | Data class |
| typeof() | Internal type |
| sum() | Addition |
| mean() | Average |
| max() | Maximum |
| min() | Minimum |
| str() | Structure |
| summary() | Summary |
| head() | First rows |
| tail() | Last rows |
| table() | Frequency |
Practical Exercises
-
Create two variables and perform all arithmetic operations.
-
Compare two numbers using comparison operators.
-
Demonstrate logical operators using TRUE and FALSE.
-
Create variables of numeric, integer, character, logical, and factor types.
-
Create a vector of 10 numbers and calculate its sum, mean, maximum, and minimum.
-
Create a list containing a student's name, age, course, and marks.
-
Create a 3×3 matrix and print the second row.
-
Create a data frame of five students with roll number, name, and marks.
-
Import a CSV file and display the first five records.
-
Create a factor for student grades and display the frequency of each grade.
Viva Questions
-
What is R?
-
What is RStudio?
-
What is the difference between R and RStudio?
-
What are the data types in R?
-
Explain vectors with an example.
-
What is a list?
-
What is a matrix?
-
What is a data frame?
-
What are factors?
-
Explain the difference between
class() and typeof().
-
What is the use of
summary()?
-
What is indexing in R?
-
How do you import a CSV file?
-
How do you export a CSV file?
-
Why are factors important in statistical analysis?
📘 Module 2: Data Manipulation and Management (10 Classes)
📚 Syllabus
1. Data Import and Export
-
Reading data from CSV files
-
Reading data from Excel files
-
Writing data to CSV files
-
Writing data to Excel files
2. Data Cleaning and Preparation
-
Handling missing values (
NA)
-
Detecting and removing duplicates
-
Data type conversion
-
Renaming rows and columns
3. Data Transformation
-
Selecting columns (
select())
-
Filtering rows (
filter())
-
Arranging data (
arrange())
-
Creating new variables (
mutate())
-
Transforming variables (
transmute())
-
Summarizing data (
summarise())
-
Grouping data (
group_by())
📖 Class-wise Course Plan
| Class | Topics |
|---|
| Class 1 | Introduction to Data Manipulation, Reading CSV Files (read.csv()) |
| Class 2 | Reading Excel Files (readxl), Importing Different File Formats |
| Class 3 | Writing Data to CSV and Excel (write.csv(), writexl) |
| Class 4 | Data Cleaning: Missing Values (NA), is.na(), na.omit() |
| Class 5 | Handling Duplicate Records, Data Type Conversion |
| Class 6 | Renaming Rows and Columns, Working with Data Frames |
| Class 7 | Data Transformation: select(), filter(), arrange() |
| Class 8 | mutate(), transmute(), Creating New Variables |
| Class 9 | summarise(), group_by(), Statistical Summaries |
| Class 10 | Complete Data Cleaning & Transformation Case Study, Revision, Viva Questions, Lab Exercises |
Class 1: Data Import and Export – Reading Data from CSV Files
Duration: 1 Class
🎯 Learning Objectives
After completing this lesson, students will be able to:
-
Understand the concept of data import.
-
Know different file formats supported by R.
-
Read CSV files into R.
-
Display and inspect imported data.
-
Understand the structure of a data frame.
-
Perform basic data exploration.
📖 2.1 Introduction to Data Import
Definition
Data Import is the process of loading data from external sources into R for analysis and visualization.
Most real-world datasets are stored in external files such as:
-
CSV Files
-
Excel Files
-
Text Files
-
JSON Files
-
Database Tables
R provides powerful functions to import these datasets efficiently.
🌟 Why Data Import is Important?
Data import is the first step in any data analysis project because it allows users to work with real-world datasets.
Advantages
-
Imports large datasets quickly.
-
Supports multiple file formats.
-
Easy to analyze imported data.
-
Compatible with data visualization and machine learning.
📊 Common Data File Formats
| File Format | Extension | Description |
|---|
| CSV | .csv | Comma-Separated Values |
| Excel | .xlsx | Microsoft Excel Workbook |
| Text | .txt | Plain Text File |
| JSON | .json | JavaScript Object Notation |
| R Data | .RData | Native R Data File |
📖 2.2 What is a CSV File?
CSV stands for Comma-Separated Values.
Each row represents one record, and each column represents one variable.
CSV is the most widely used format for data exchange because it is simple and supported by almost every software application.
📊 Sample CSV Dataset (10 Records)
File Name: employee.csv
| Emp_ID | Name | Department | Age | Salary |
|---|
| 101 | Amit | HR | 25 | 30000 |
| 102 | Priya | Sales | 28 | 35000 |
| 103 | Rahul | IT | 30 | 50000 |
| 104 | Sneha | HR | 27 | 32000 |
| 105 | Karan | IT | 35 | 60000 |
| 106 | Neha | Finance | 31 | 55000 |
| 107 | Arjun | Sales | 29 | 40000 |
| 108 | Pooja | Finance | 33 | 58000 |
| 109 | Rohan | IT | 26 | 45000 |
| 110 | Anjali | HR | 32 | 52000 |
📖 2.3 Creating a CSV File
The dataset above can be saved in Notepad or Microsoft Excel as:
CSV Content
Emp_ID,Name,Department,Age,Salary
101,Amit,HR,25,30000
102,Priya,Sales,28,35000
103,Rahul,IT,30,50000
104,Sneha,HR,27,32000
105,Karan,IT,35,60000
106,Neha,Finance,31,55000
107,Arjun,Sales,29,40000
108,Pooja,Finance,33,58000
109,Rohan,IT,26,45000
110,Anjali,HR,32,52000
🔵 2.4 Reading a CSV File
Method 1: Using read.csv()
Syntax
Parameters
| Parameter | Description |
|---|
file | CSV file path |
header | TRUE if the first row contains column names |
💻 Example 1: Read Employee Data
Output
Explanation
-
read.csv() imports the CSV file.
-
The data is stored as a data frame.
-
Each row represents one employee.
-
Each column represents one variable.
💻 Example 2: View the First Six Records
Output
💻 Example 3: View the Last Six Records
Output
💻 Example 4: Display Structure of Dataset
Output
Explanation
str() displays:
-
Number of rows
-
Number of columns
-
Data types of variables
💻 Example 5: Dataset Dimensions
Output
Interpretation: The dataset contains 10 rows and 5 columns.
💻 Example 6: Column Names
Output
💻 Example 7: Row Names
Output
💻 Example 8: Summary of Dataset
Output (Example)
💻 Example 9: Display Individual Column
Output
💻 Example 10: Display Multiple Columns
Output
📊 Common Functions for Exploring Data
| Function | Purpose |
|---|
head() | First 6 rows |
tail() | Last 6 rows |
str() | Structure |
summary() | Statistical summary |
dim() | Rows and columns |
nrow() | Number of rows |
ncol() | Number of columns |
colnames() | Column names |
rownames() | Row names |
🌍 Real-Life Applications
-
Importing student records
-
Employee databases
-
Sales reports
-
Banking transactions
-
Hospital patient data
-
Survey results
-
Research datasets
-
Machine learning datasets
✔ Advantages of CSV Files
-
Easy to create and edit.
-
Lightweight and portable.
-
Supported by Excel, R, Python, and databases.
-
Ideal for data exchange.
✖ Limitations
-
Does not store formatting.
-
Does not support formulas.
-
No multiple worksheets (unlike Excel).
-
Data types are not preserved automatically.
📝 Lab Exercises
-
Create an
employee.csv file with 10 employee records.
-
Import the file using
read.csv().
-
Display the first and last six records.
-
Find the number of rows and columns.
-
Display the structure of the dataset.
-
Print only the
Name and Salary columns.
-
Generate a statistical summary using
summary().
❓ Viva Questions
-
What is a CSV file?
-
What is the purpose of
read.csv()?
-
What does the
header argument do?
-
Which function displays the first six rows?
-
Which function shows the structure of a dataset?
-
How do you display column names?
-
What is the difference between
head() and tail()?
-
What information does
summary() provide?
-
Name two advantages of CSV files.
-
Give two real-world applications of importing CSV data.
📚 Class Summary
In this class, you learned:
-
The concept of data import.
-
CSV file structure.
-
Reading CSV files using
read.csv().
-
Exploring datasets with
head(), tail(), str(), dim(), and summary().
-
Practical examples using a 10-record employee dataset.
-
Real-world applications, advantages, limitations, exercises, and viva questions.
Class 2: Data Import and Export – Reading Data from Excel Files (.xlsx)
Duration: 1 Class
🎯 Learning Objectives
After completing this lesson, students will be able to:
-
Understand Excel file formats.
-
Install and use the readxl package.
-
Read Excel files into R.
-
Import specific worksheets.
-
Read multiple sheets from an Excel workbook.
-
Explore imported data using R functions.
-
Compare CSV and Excel file formats.
📖 2.5 Introduction to Excel Files
Definition
An Excel file is a spreadsheet created using Microsoft Excel. It stores data in rows and columns and may contain multiple worksheets, formulas, charts, and formatting.
Unlike CSV files, Excel files can store multiple sheets in a single workbook.
🌟 Advantages of Excel Files
-
Multiple worksheets in one file
-
Supports formulas and functions
-
Can contain charts and graphs
-
Easy to edit using Microsoft Excel
-
Widely used in businesses and organizations
📊 Excel File Extensions
| Extension | Description |
|---|
.xls | Excel 97–2003 Workbook |
.xlsx | Excel 2007 and Later Workbook |
.xlsm | Macro-Enabled Workbook |
📖 2.6 The readxl Package
The readxl package is used to import Excel files into R.
If it is not installed, install it once using:
Install Package
Load Package
📊 Sample Excel File
File Name: employee.xlsx
Worksheet: Employee
| Emp_ID | Name | Department | Age | Salary |
|---|
| 101 | Amit | HR | 25 | 30000 |
| 102 | Priya | Sales | 28 | 35000 |
| 103 | Rahul | IT | 30 | 50000 |
| 104 | Sneha | HR | 27 | 32000 |
| 105 | Karan | IT | 35 | 60000 |
| 106 | Neha | Finance | 31 | 55000 |
| 107 | Arjun | Sales | 29 | 40000 |
| 108 | Pooja | Finance | 33 | 58000 |
| 109 | Rohan | IT | 26 | 45000 |
| 110 | Anjali | HR | 32 | 52000 |
📖 2.7 Reading an Excel File
Syntax
💻 Example 1: Read an Excel File
Output
💻 Example 2: Read a Specific Worksheet
Suppose the workbook contains two sheets:
Output
Displays all records from the Employee worksheet.
💻 Example 3: Read Sheet by Number
Output
Imports the first worksheet.
💻 Example 4: Display Available Sheet Names
Output
💻 Example 5: Read Selected Columns
Output
💻 Example 6: Read Specific Cell Range
Output
Imports only the first six rows.
💻 Example 7: View Dataset Structure
Output
💻 Example 8: Display Summary
Output
💻 Example 9: First Six Records
Output
💻 Example 10: Last Six Records
Output
📊 Comparison: CSV vs Excel
| Feature | CSV | Excel |
|---|
| File Extension | .csv | .xlsx |
| Multiple Sheets | ❌ No | ✅ Yes |
| Supports Formatting | ❌ No | ✅ Yes |
| Supports Charts | ❌ No | ✅ Yes |
| File Size | Small | Larger |
| Speed | Faster | Slightly Slower |
| Best For | Data Exchange | Business Reports |
🌍 Real-Life Applications
-
Student attendance records
-
Employee payroll
-
Banking reports
-
Hospital patient data
-
Sales reports
-
Inventory management
-
Research datasets
-
Financial statements
✔ Advantages of readxl
-
Reads Excel files directly.
-
Supports
.xls and .xlsx.
-
Imports selected sheets.
-
Imports selected cell ranges.
-
Fast and reliable.
✖ Limitations
-
Cannot modify Excel files (reading only).
-
Formatting is not imported.
-
Macros are ignored.
-
Charts and images are not imported.
📝 Lab Exercises
Exercise 1
Install the readxl package.
Exercise 2
Read an Excel file named employee.xlsx.
Exercise 3
Display available worksheet names.
Exercise 4
Read only the first worksheet.
Exercise 5
Import only columns A to C.
Exercise 6
Import rows 1–6 from the worksheet.
Exercise 7
Display the structure and summary of the imported dataset.
❓ Viva Questions
-
What is an Excel workbook?
-
Which package is used to read Excel files in R?
-
Which function imports Excel data?
-
What is the purpose of
excel_sheets()?
-
How do you read a worksheet by name?
-
How do you read a worksheet by number?
-
What is the difference between CSV and Excel?
-
Can
readxl read .xls files?
-
Can
readxl import charts?
-
Give two applications of Excel data import.
📚 Class Summary
In this class, you learned:
-
Introduction to Excel files.
-
Installing and loading the readxl package.
-
Reading Excel files with
read_excel().
-
Importing specific worksheets and ranges.
-
Viewing sheet names with
excel_sheets().
-
Comparing CSV and Excel formats.
-
Practical R programs with outputs.
-
Real-world applications, exercises, and viva questions.
Class 3: Data Export – Writing Data to CSV and Excel Files
Duration: 1 Class
🎯 Learning Objectives
After completing this lesson, students will be able to:
-
Understand data export in R.
-
Write data frames to CSV files.
-
Write data frames to Excel files.
-
Export selected columns and filtered data.
-
Save processed data for future use.
-
Understand the differences between CSV and Excel exports.
📖 2.8 Introduction to Data Export
Definition
Data Export is the process of saving data from R into an external file so that it can be used in other software such as Microsoft Excel, LibreOffice Calc, databases, or shared with others.
Common export formats include:
-
CSV (.csv)
-
Excel (.xlsx)
-
Text (.txt)
-
RData (.RData)
🌟 Why Data Export is Important?
Data export allows users to:
-
Save processed datasets.
-
Share reports with others.
-
Store analysis results.
-
Create backup copies.
-
Use data in other applications.
📊 Sample Dataset (10 Records)
employee <- data.frame(
Emp_ID=c(101,102,103,104,105,106,107,108,109,110),
Name=c("Amit","Priya","Rahul","Sneha","Karan",
"Neha","Arjun","Pooja","Rohan","Anjali"),
Department=c("HR","Sales","IT","HR","IT",
"Finance","Sales","Finance","IT","HR"),
Age=c(25,28,30,27,35,31,29,33,26,32),
Salary=c(30000,35000,50000,32000,60000,
55000,40000,58000,45000,52000)
)
employee
Output
🔵 2.9 Writing Data to a CSV File
Syntax
Parameters
| Parameter | Description |
|---|
data | Data frame to export |
file | Output file name |
row.names=FALSE | Prevents row numbers from being written |
💻 Example 1: Export Entire Dataset
Output
💻 Example 2: Export Selected Columns
Output
💻 Example 3: Export Employees from IT Department
Output
💻 Example 4: Export Employees with Salary > 50,000
Output
🟢 2.10 Writing Data to Excel Files
R uses the writexl package to export Excel files.
Install Package
Load Package
Syntax
💻 Example 5: Export to Excel
Output
💻 Example 6: Export Salary Data
Output
💻 Example 7: Export HR Department
Output
💻 Example 8: Export Finance Department
Output
💻 Example 9: Export Employees Older Than 30
Output
💻 Example 10: Export Summary Statistics
Output
📊 Comparison: write.csv() vs write_xlsx()
| Feature | write.csv() | write_xlsx() |
|---|
| Output Format | CSV | Excel |
| Multiple Sheets | ❌ No | ❌ No (basic usage) |
| File Size | Smaller | Larger |
| Readable in Excel | ✅ Yes | ✅ Yes |
| Supports Formatting | ❌ No | Limited |
🌍 Real-Life Applications
-
Exporting employee payroll reports.
-
Saving student examination results.
-
Generating monthly sales reports.
-
Creating financial statements.
-
Exporting survey responses.
-
Sharing machine learning results.
-
Backing up processed datasets.
-
Sending reports to management.
✔ Advantages
-
Saves processed data permanently.
-
Easy to share with others.
-
Compatible with Excel and other software.
-
Useful for report generation.
-
Supports automation.
✖ Limitations
-
CSV files cannot store formatting.
-
Excel export requires an additional package.
-
Charts and formulas are not exported automatically.
📝 Lab Exercises
-
Create a data frame containing 10 student records.
-
Export the data frame to a CSV file.
-
Export only the
Name and Marks columns.
-
Export students scoring more than 80 marks.
-
Export the dataset to an Excel file.
-
Create separate Excel files for different departments.
-
Generate a summary report and save it as a text file.
❓ Viva Questions
-
What is data export?
-
Which function exports data to CSV?
-
Why is
row.names = FALSE commonly used?
-
Which package is used to export Excel files?
-
What is the purpose of
write_xlsx()?
-
Can CSV files store formatting?
-
Name two advantages of exporting data.
-
What is the difference between CSV and Excel export?
-
How can you export only selected columns?
-
Give two real-life applications of data export.
📚 Class Summary
In this class, you learned:
-
The concept of data export.
-
Writing data frames to CSV files using
write.csv().
-
Writing Excel files using the
writexl package.
-
Exporting filtered and selected datasets.
-
Comparison of CSV and Excel exports.
-
Practical examples with outputs.
-
Real-world applications, lab exercises, and viva questions.
Class 4: Data Cleaning and Preparation – Handling Missing Values (NA)
Duration: 1 Class
🎯 Learning Objectives
After completing this lesson, students will be able to:
-
Understand missing values (
NA) in R.
-
Identify missing values in datasets.
-
Count missing values.
-
Remove missing values.
-
Replace missing values.
-
Perform statistical analysis after handling missing data.
📖 2.11 Introduction to Data Cleaning
Definition
Data Cleaning is the process of detecting, correcting, or removing inaccurate, incomplete, duplicate, or inconsistent data from a dataset.
Data cleaning is one of the most important steps in Data Science, Machine Learning, and Statistical Analysis because the quality of the analysis depends on the quality of the data.
🌟 Why Data Cleaning is Important?
Data cleaning helps to:
-
Improve data quality.
-
Increase the accuracy of analysis.
-
Remove errors and inconsistencies.
-
Handle missing values effectively.
-
Improve machine learning model performance.
📖 2.12 What are Missing Values?
A missing value is a data value that is unavailable or unknown. In R, missing values are represented by NA (Not Available).
Common Causes of Missing Values
-
Data entry errors
-
Survey respondents skipping questions
-
Equipment or sensor failures
-
Data transmission errors
-
Incomplete records
📊 Sample Dataset (10 Records)
Output
📖 2.13 Detecting Missing Values
Syntax
is.na() checks each value and returns TRUE if it is missing, otherwise FALSE.
💻 Example 1: Detect Missing Values
Output
💻 Example 2: Count Missing Values
Output
Explanation: There are 4 missing values in the dataset.
💻 Example 3: Missing Values in Each Column
Output
💻 Example 4: Missing Values in Each Row
Output
📖 2.14 Removing Missing Values
Syntax
💻 Example 5: Remove Missing Records
Output
Explanation
Rows containing missing values are removed.
📖 2.15 Replacing Missing Values
Instead of deleting rows, missing values can be replaced.
💻 Example 6: Replace Missing Marks with Zero
Output
💻 Example 7: Replace Missing Age with Mean Age
Output
Explanation
na.rm=TRUE ignores missing values while calculating the mean.
💻 Example 8: Calculate Mean Without Missing Values
Output
💻 Example 9: Calculate Median
Output
💻 Example 10: Standard Deviation
Output
(Approximate value.)
📖 2.16 Methods for Handling Missing Values
| Method | Description |
|---|
| Delete rows | Remove incomplete records |
| Replace with Mean | Numerical data |
| Replace with Median | Skewed numerical data |
| Replace with Mode | Categorical data |
| Predict Missing Values | Machine learning techniques |
📊 Useful Functions
| Function | Purpose |
|---|
is.na() | Detect missing values |
sum(is.na()) | Count missing values |
colSums(is.na()) | Missing values by column |
rowSums(is.na()) | Missing values by row |
na.omit() | Remove missing rows |
mean(..., na.rm=TRUE) | Ignore missing values |
median(..., na.rm=TRUE) | Ignore missing values |
sd(..., na.rm=TRUE) | Standard deviation without missing values |
🌍 Real-Life Applications
-
Student attendance records
-
Hospital patient databases
-
Banking transactions
-
Insurance claims
-
Sales and inventory management
-
Customer feedback analysis
-
Survey data cleaning
-
Machine learning preprocessing
✔ Advantages
-
Improves data quality.
-
Increases analysis accuracy.
-
Prevents errors in statistical calculations.
-
Enhances model performance.
-
Produces reliable reports.
✖ Disadvantages
-
Removing records may reduce dataset size.
-
Replacing values may introduce bias if done incorrectly.
-
Requires careful selection of imputation methods.
📝 Lab Exercises
-
Create a dataset with 10 student records containing missing values.
-
Detect missing values using
is.na().
-
Count total missing values.
-
Find missing values in each column.
-
Find missing values in each row.
-
Remove missing records using
na.omit().
-
Replace missing marks with 0.
-
Replace missing ages with the mean age.
-
Calculate the mean and median while ignoring missing values.
-
Find the standard deviation of marks after handling missing values.
❓ Viva Questions
-
What is a missing value in R?
-
How are missing values represented in R?
-
What is the purpose of
is.na()?
-
What does
na.omit() do?
-
Why is
na.rm=TRUE used?
-
How can missing values be counted?
-
What are common causes of missing data?
-
When should you replace missing values instead of deleting rows?
-
What are the advantages of handling missing values?
-
Give two real-life applications of data cleaning.
📚 Class Summary
In this class, you learned:
-
The concept of data cleaning.
-
Missing values (
NA) and their causes.
-
Detecting missing values using
is.na().
-
Counting missing values.
-
Removing missing records with
na.omit().
-
Replacing missing values with constants and statistical measures.
-
Practical examples with outputs.
-
Real-world applications, exercises, and viva questions.
Class 5: Handling Duplicate Records and Data Type Conversion
Duration: 1 Class
🎯 Learning Objectives
After completing this lesson, students will be able to:
-
Understand duplicate records in datasets.
-
Detect duplicate rows and values.
-
Remove duplicate records.
-
Understand different data types in R.
-
Convert data between numeric, character, factor, and logical types.
-
Apply data type conversion in real-world datasets.
📖 2.17 Introduction to Duplicate Data
Definition
A duplicate record is a row or value that appears more than once in a dataset.
Duplicate data may occur because of:
-
Repeated data entry
-
System errors
-
Database merging
-
Data import from multiple sources
Duplicate records can lead to inaccurate statistical analysis and incorrect reports.
🌟 Why Remove Duplicate Records?
Removing duplicates helps to:
-
Improve data quality.
-
Reduce storage space.
-
Increase analysis accuracy.
-
Prevent incorrect statistical results.
-
Improve machine learning performance.
📊 Sample Dataset (10 Records)
Output
📖 2.18 Detecting Duplicate Records
Syntax
💻 Example 1: Detect Duplicate Rows
Output
Explanation
The 6th row is a duplicate of the 3rd row.
💻 Example 2: Display Duplicate Records
Output
💻 Example 3: Count Duplicate Records
Output
💻 Example 4: Remove Duplicate Records
Output
💻 Example 5: Detect Duplicate Employee IDs
Output
📖 2.19 Data Types in R
R supports different types of data.
| Data Type | Description | Example |
|---|
| Numeric | Numbers | 100 |
| Character | Text | "Amit" |
| Logical | TRUE/FALSE | TRUE |
| Factor | Categories | HR, Sales |
📖 2.20 Data Type Conversion
Data type conversion changes one data type into another.
💻 Example 6: Numeric to Character
Output
💻 Example 7: Character to Numeric
Output
💻 Example 8: Character to Factor
Output
💻 Example 9: Numeric to Logical
Output
Explanation
-
0 becomes FALSE.
-
Any non-zero value becomes TRUE.
💻 Example 10: Check Data Type
Output
📊 Common Conversion Functions
| Function | Purpose |
|---|
as.numeric() | Convert to numeric |
as.character() | Convert to character |
as.factor() | Convert to factor |
as.logical() | Convert to logical |
class() | Display data type |
str() | Display structure |
📊 Comparison of Data Types
| Type | Stores | Example |
|---|
| Numeric | Numbers | 100 |
| Character | Text | "Amit" |
| Logical | TRUE/FALSE | TRUE |
| Factor | Categories | HR |
🌍 Real-Life Applications
Duplicate Handling
-
Banking transactions
-
Employee databases
-
Hospital patient records
-
Student admission systems
-
Customer databases
Data Type Conversion
-
Machine learning preprocessing
-
Survey analysis
-
Statistical modeling
-
Financial analysis
-
Database management
✔ Advantages
-
Removes redundant information.
-
Improves dataset quality.
-
Ensures correct data types for analysis.
-
Enhances model accuracy.
-
Simplifies data manipulation.
✖ Disadvantages
-
Removing duplicates without verification may delete valid records.
-
Incorrect data type conversion may cause data loss.
-
Requires careful validation before conversion.
📝 Lab Exercises
-
Create a dataset containing duplicate employee records.
-
Detect duplicate rows using
duplicated().
-
Count duplicate records.
-
Remove duplicate records.
-
Detect duplicate employee IDs.
-
Convert numeric data to character.
-
Convert character data to numeric.
-
Convert department names to factors.
-
Convert numeric values to logical.
-
Display the structure of the dataset.
❓ Viva Questions
-
What is a duplicate record?
-
Which function detects duplicate rows?
-
How can duplicate rows be removed?
-
What is the purpose of
duplicated()?
-
What are the four basic data types in R?
-
Which function converts data to numeric?
-
Which function converts data to character?
-
What is a factor in R?
-
How does
as.logical() work?
-
Why is data type conversion important?
📚 Class Summary
In this class, you learned:
-
Duplicate records and their effects.
-
Detecting and removing duplicate data.
-
Basic data types in R.
-
Data type conversion using
as.numeric(), as.character(), as.factor(), and as.logical().
-
Practical examples with outputs.
-
Real-world applications, exercises, and viva questions.
Class 6: Renaming Columns and Rows in R
Duration: 1 Class
🎯 Learning Objectives
After completing this lesson, students will be able to:
-
Understand the importance of meaningful column and row names.
-
Rename columns using
colnames(), names(), and rename().
-
Rename rows using
rownames().
-
Rename multiple columns simultaneously.
-
Apply renaming techniques in real-world datasets.
📖 2.21 Introduction to Renaming
Definition
Renaming is the process of changing the names of columns or rows in a dataset to make them more meaningful, readable, and easier to understand.
For example:
| Old Name | New Name |
|---|
| M1 | Marks |
| Dept | Department |
| Sal | Salary |
| Age1 | Age |
Using meaningful names improves code readability and makes data analysis easier.
🌟 Why Rename Columns and Rows?
Renaming helps to:
-
Improve readability.
-
Use meaningful variable names.
-
Avoid confusion during analysis.
-
Make reports easier to understand.
-
Prepare data for machine learning and visualization.
📊 Sample Dataset (10 Records)
employee <- data.frame(
ID=c(101,102,103,104,105,106,107,108,109,110),
EmpName=c("Amit","Priya","Rahul","Sneha","Karan",
"Neha","Arjun","Pooja","Rohan","Anjali"),
Dept=c("HR","Sales","IT","HR","IT",
"Finance","Sales","Finance","IT","HR"),
Age=c(25,28,30,27,35,31,29,33,26,32),
Sal=c(30000,35000,50000,32000,60000,
55000,40000,58000,45000,52000)
)
employee
Output
| ID | EmpName | Dept | Age | Sal |
|---|
| 101 | Amit | HR | 25 | 30000 |
| 102 | Priya | Sales | 28 | 35000 |
| 103 | Rahul | IT | 30 | 50000 |
| 104 | Sneha | HR | 27 | 32000 |
| 105 | Karan | IT | 35 | 60000 |
| 106 | Neha | Finance | 31 | 55000 |
| 107 | Arjun | Sales | 29 | 40000 |
| 108 | Pooja | Finance | 33 | 58000 |
| 109 | Rohan | IT | 26 | 45000 |
| 110 | Anjali | HR | 32 | 52000 |
📖 2.22 Renaming Columns Using colnames()
Syntax
💻 Example 1: Rename All Columns
Output
| Emp_ID | Name | Department | Age | Salary |
|---|
| 101 | Amit | HR | 25 | 30000 |
| 102 | Priya | Sales | 28 | 35000 |
| 103 | Rahul | IT | 30 | 50000 |
| ... | ... | ... | ... | ... |
💻 Example 2: Display Column Names
Output
📖 2.23 Renaming Columns Using names()
names() works similarly to colnames().
Syntax
💻 Example 3
Output
💻 Example 4: Rename One Column
Output
| Emp_ID | Name | Department | Age | Monthly_Salary |
|---|
| 101 | Amit | HR | 25 | 30000 |
| 102 | Priya | Sales | 28 | 35000 |
| ... | ... | ... | ... | ... |
📖 2.24 Renaming Rows
Rows can also have names.
Syntax
💻 Example 5: Display Row Names
Output
💻 Example 6: Rename Rows
Output
📖 2.25 Renaming Using rename() from dplyr
The dplyr package provides the rename() function.
Install Package
Load Package
Syntax
💻 Example 7
Output
The column Monthly_Salary is renamed to Salary.
💻 Example 8: Rename Multiple Columns
Output
| Employee_ID | Employee_Name | Department | Age | Salary |
|---|
| 101 | Amit | HR | 25 | 30000 |
| 102 | Priya | Sales | 28 | 35000 |
| ... | ... | ... | ... | ... |
💻 Example 9: Verify Structure
Output
💻 Example 10: Display Dataset
Output
📊 Comparison of Renaming Functions
| Function | Purpose |
|---|
colnames() | Rename all columns |
names() | Rename one or more columns |
rownames() | Rename rows |
rename() | Rename selected columns using dplyr |
📊 Advantages of Meaningful Column Names
| Poor Name | Better Name |
|---|
| M1 | Marks |
| Dept | Department |
| Sal | Salary |
| Emp | Employee_Name |
| ID | Employee_ID |
🌍 Real-Life Applications
-
Employee management systems
-
Student databases
-
Banking records
-
Hospital patient databases
-
Inventory management
-
Sales reporting
-
Data visualization
-
Machine learning preprocessing
✔ Advantages
-
Improves readability.
-
Makes code easier to understand.
-
Helps create professional reports.
-
Simplifies data manipulation.
-
Enhances collaboration among team members.
✖ Limitations
-
Renaming columns incorrectly may break existing code.
-
Duplicate column names should be avoided.
-
Frequent renaming may reduce code consistency.
📝 Lab Exercises
-
Create a dataset containing 10 employee records.
-
Rename all column names using
colnames().
-
Display column names.
-
Rename one column using
names().
-
Display row names.
-
Rename all row names.
-
Install and load the dplyr package.
-
Rename one column using
rename().
-
Rename two columns simultaneously.
-
Display the structure of the renamed dataset.
❓ Viva Questions
-
What is the purpose of renaming columns?
-
Which function changes column names?
-
Which function changes row names?
-
What is the difference between
colnames() and names()?
-
Which package contains
rename()?
-
How do you rename multiple columns?
-
Why are meaningful column names important?
-
Can row names be customized?
-
What is the syntax of
rename()?
-
Give two real-life applications of renaming data.
📚 Class Summary
In this class, you learned:
-
The importance of meaningful column and row names.
-
Renaming columns using
colnames() and names().
-
Renaming rows using
rownames().
-
Using
rename() from the dplyr package.
-
Practical examples with outputs.
-
Comparison tables, real-world applications, lab exercises, and viva questions.
Class 7: Data Transformation – select(), filter(), and arrange()
Duration: 1 Class
🎯 Learning Objectives
After completing this lesson, students will be able to:
Select specific columns from a dataset.
Filter rows based on conditions.
Sort data in ascending and descending order.
Combine multiple transformation operations.
Use dplyr functions for efficient data analysis.
📖 2.26 Introduction to Data Transformation
Data Transformation means modifying, selecting, filtering, or arranging data into a form suitable for analysis.
R provides powerful transformation functions through the dplyr package.
📊 Sample Dataset (10 Records)
🔵 2.27 Selecting Columns with select()
Definition
The select() function chooses specific columns from a dataset.
💻 Example 1: Select Name and Salary
Name | Salary |
|---|
Amit | 30000 |
Priya | 35000 |
Rahul | 50000 |
... | ... |
💻 Example 2: Select Multiple Columns
💻 Example 3: Exclude a Column
🟢 2.28 Filtering Rows with filter()
Definition
The filter() function selects rows that satisfy specified conditions.
💻 Example 4: Employees from IT Department
Name | Department |
|---|
Rahul | IT |
Karan | IT |
Rohan | IT |
💻 Example 5: Salary Greater Than 50,000
💻 Example 6: Multiple Conditions (AND)
💻 Example 7: Multiple Conditions (OR)
🟣 2.29 Arranging Data with arrange()
Definition
The arrange() function sorts rows based on one or more columns.
💻 Example 8: Sort by Salary (Ascending)
Name | Salary |
|---|
Amit | 30000 |
Sneha | 32000 |
Priya | 35000 |
... | ... |
💻 Example 9: Sort by Salary (Descending)
Name | Salary |
|---|
Karan | 60000 |
Pooja | 58000 |
Neha | 55000 |
... | ... |
💻 Example 10: Sort by Department and Salary
📊 Combining Functions
Example: IT Employees Sorted by Salary
Name | Salary |
|---|
Karan | 60000 |
Rahul | 50000 |
Rohan | 45000 |
📊 Comparison of Functions
Function | Purpose |
|---|
select() | Choose columns |
filter() | Choose rows |
arrange() | Sort rows |
🌍 Real-Life Applications
Selecting important columns from large databases.
Filtering customers with high purchases.
Sorting employees by salary.
Analyzing sales by region.
Preparing data for machine learning.
Generating management reports.
✔ Advantages
Simple and readable syntax.
Works well with large datasets.
Easy to combine multiple operations.
Widely used in data science projects.
✖ Limitations
Requires the dplyr package.
Very large datasets may require additional optimization.
Incorrect conditions may produce unexpected results.
📝 Lab Exercises
Select only Name and Salary columns.
Filter employees from the Sales department.
Filter employees with salary greater than 40,000.
Filter employees from IT with salary greater than 45,000.
Sort employees by Age ascending.
Sort employees by Salary descending.
Sort employees by Department and Salary.
Display only IT employees sorted by salary.
Combine select(), filter(), and arrange() in one program.
❓ Viva Questions
What is data transformation?
What is the purpose of select()?
What is the purpose of filter()?
What is the purpose of arrange()?
How do you sort data in descending order?
How do you apply multiple conditions in filter()?
Can select() exclude columns?
What is the pipe operator %>%?
Give two real-life applications of data transformation.
📚 Class Summary
In this class, you learned:
select() for choosing columns.
filter() for selecting rows.
arrange() for sorting data.
Using multiple conditions.
Combining transformation functions with the pipe operator.
Practical examples with outputs.
Real-world applications, exercises, and viva questions.
Class 8: Data Transformation using mutate() and transmute()
Duration: 1 Class
🎯 Learning Objectives
After completing this lesson, students will be able to:
-
Understand the purpose of
mutate() and transmute().
-
Create new variables in a dataset.
-
Modify existing variables.
-
Perform arithmetic operations on columns.
-
Calculate bonus, tax, gross salary, and net salary.
-
Understand the difference between
mutate() and transmute().
📖 2.30 Introduction to mutate()
Definition
The mutate() function from the dplyr package is used to create new columns or modify existing columns in a data frame.
It is one of the most frequently used functions in data analysis and machine learning.
Install and Load Package
📊 Sample Dataset (10 Records)
employee <- data.frame(
Emp_ID=c(101,102,103,104,105,106,107,108,109,110),
Name=c("Amit","Priya","Rahul","Sneha","Karan",
"Neha","Arjun","Pooja","Rohan","Anjali"),
Department=c("HR","Sales","IT","HR","IT",
"Finance","Sales","Finance","IT","HR"),
Age=c(25,28,30,27,35,31,29,33,26,32),
Salary=c(30000,35000,50000,32000,60000,
55000,40000,58000,45000,52000)
)
employee
Output
| Emp_ID | Name | Department | Age | Salary |
|---|
| 101 | Amit | HR | 25 | 30000 |
| 102 | Priya | Sales | 28 | 35000 |
| 103 | Rahul | IT | 30 | 50000 |
| 104 | Sneha | HR | 27 | 32000 |
| 105 | Karan | IT | 35 | 60000 |
| 106 | Neha | Finance | 31 | 55000 |
| 107 | Arjun | Sales | 29 | 40000 |
| 108 | Pooja | Finance | 33 | 58000 |
| 109 | Rohan | IT | 26 | 45000 |
| 110 | Anjali | HR | 32 | 52000 |
📖 2.31 Creating New Columns with mutate()
Syntax
💻 Example 1: Calculate 10% Bonus
Output
| Name | Salary | Bonus |
|---|
| Amit | 30000 | 3000 |
| Priya | 35000 | 3500 |
| Rahul | 50000 | 5000 |
| Sneha | 32000 | 3200 |
| Karan | 60000 | 6000 |
| Neha | 55000 | 5500 |
| Arjun | 40000 | 4000 |
| Pooja | 58000 | 5800 |
| Rohan | 45000 | 4500 |
| Anjali | 52000 | 5200 |
💻 Example 2: Calculate Gross Salary
Output
| Name | Salary | Gross_Salary |
|---|
| Amit | 30000 | 33000 |
| Priya | 35000 | 38500 |
| Rahul | 50000 | 55000 |
| Sneha | 32000 | 35200 |
| Karan | 60000 | 66000 |
| Neha | 55000 | 60500 |
| Arjun | 40000 | 44000 |
| Pooja | 58000 | 63800 |
| Rohan | 45000 | 49500 |
| Anjali | 52000 | 57200 |
💻 Example 3: Calculate 5% Income Tax
Output
| Name | Salary | Tax |
|---|
| Amit | 30000 | 1500 |
| Priya | 35000 | 1750 |
| Rahul | 50000 | 2500 |
| Sneha | 32000 | 1600 |
| Karan | 60000 | 3000 |
| Neha | 55000 | 2750 |
| Arjun | 40000 | 2000 |
| Pooja | 58000 | 2900 |
| Rohan | 45000 | 2250 |
| Anjali | 52000 | 2600 |
💻 Example 4: Calculate Net Salary
Output
| Name | Salary | Bonus | Tax | Net_Salary |
|---|
| Amit | 30000 | 3000 | 1500 | 31500 |
| Priya | 35000 | 3500 | 1750 | 36750 |
| Rahul | 50000 | 5000 | 2500 | 52500 |
| Sneha | 32000 | 3200 | 1600 | 33600 |
| Karan | 60000 | 6000 | 3000 | 63000 |
| Neha | 55000 | 5500 | 2750 | 57750 |
| Arjun | 40000 | 4000 | 2000 | 42000 |
| Pooja | 58000 | 5800 | 2900 | 60900 |
| Rohan | 45000 | 4500 | 2250 | 47250 |
| Anjali | 52000 | 5200 | 2600 | 54600 |
💻 Example 5: Increase Salary by ₹5,000
Output
Each employee's salary increases by ₹5,000.
📖 2.32 The transmute() Function
Definition
The transmute() function creates new columns but returns only the newly created columns.
Unlike mutate(), the original columns are not included.
Syntax
💻 Example 6: Display Bonus Only
Output
| Name | Bonus |
|---|
| Amit | 3000 |
| Priya | 3500 |
| Rahul | 5000 |
| Sneha | 3200 |
| Karan | 6000 |
| Neha | 5500 |
| Arjun | 4000 |
| Pooja | 5800 |
| Rohan | 4500 |
| Anjali | 5200 |
💻 Example 7: Gross Salary Only
Output
Displays only Name and Gross Salary.
💻 Example 8: Age After Five Years
Output
| Name | Age | Age_After_5_Years |
|---|
| Amit | 25 | 30 |
| Priya | 28 | 33 |
| Rahul | 30 | 35 |
| Sneha | 27 | 32 |
| Karan | 35 | 40 |
| Neha | 31 | 36 |
| Arjun | 29 | 34 |
| Pooja | 33 | 38 |
| Rohan | 26 | 31 |
| Anjali | 32 | 37 |
💻 Example 9: Annual Salary
Output
| Name | Monthly Salary | Annual Salary |
|---|
| Amit | 30000 | 360000 |
| Priya | 35000 | 420000 |
| Rahul | 50000 | 600000 |
| Sneha | 32000 | 384000 |
| Karan | 60000 | 720000 |
| Neha | 55000 | 660000 |
| Arjun | 40000 | 480000 |
| Pooja | 58000 | 696000 |
| Rohan | 45000 | 540000 |
| Anjali | 52000 | 624000 |
💻 Example 10: Employee Category
Output
| Name | Salary | Category |
|---|
| Amit | 30000 | Normal Salary |
| Priya | 35000 | Normal Salary |
| Rahul | 50000 | High Salary |
| Sneha | 32000 | Normal Salary |
| Karan | 60000 | High Salary |
| Neha | 55000 | High Salary |
| Arjun | 40000 | Normal Salary |
| Pooja | 58000 | High Salary |
| Rohan | 45000 | Normal Salary |
| Anjali | 52000 | High Salary |
📊 Comparison of mutate() and transmute()
| Feature | mutate() | transmute() |
|---|
| Keeps Original Columns | ✅ Yes | ❌ No |
| Creates New Columns | ✅ Yes | ✅ Yes |
| Modifies Existing Columns | ✅ Yes | ✅ Yes |
| Returns Only New Columns | ❌ No | ✅ Yes |
🌍 Real-Life Applications
-
Employee payroll systems
-
Student result processing
-
Banking interest calculation
-
GST and tax calculation
-
Insurance premium calculation
-
Sales commission reports
-
Financial reporting
-
Business analytics
📝 Lab Exercises
-
Calculate a 15% bonus for each employee.
-
Create a Gross Salary column.
-
Create a Net Salary column after deducting 8% tax.
-
Calculate annual salary.
-
Increase every salary by ₹2,000.
-
Create a category column (High Salary, Medium Salary, Low Salary).
-
Display only Name and Bonus using
transmute().
-
Calculate age after 10 years.
-
Create a PF deduction column (12% of salary).
-
Calculate Take Home Salary = Salary + Bonus − Tax − PF.
❓ Viva Questions
-
What is the purpose of
mutate()?
-
What is the difference between
mutate() and transmute()?
-
Can
mutate() modify existing columns?
-
Which package contains
mutate()?
-
Which function returns only new columns?
-
How do you create a new column in R?
-
What is the use of
ifelse() inside mutate()?
-
How do you calculate annual salary?
-
What are the advantages of
mutate()?
-
Give two real-life applications of
transmute().
📚 Class Summary
In this class, you learned:
-
Creating new variables with
mutate().
-
Modifying existing variables.
-
Using
transmute() to return only selected transformed columns.
-
Calculating bonus, tax, gross salary, net salary, annual salary, and employee categories.
-
Practical R programs with outputs.
-
Real-world applications, lab exercises, and viva questions.
Class 9: Data Transformation using summarise() and group_by()
Duration: 1 Class
🎯 Learning Objectives
After completing this lesson, students will be able to:
-
Understand the purpose of
summarise() and group_by().
-
Calculate statistical summaries of datasets.
-
Group data based on one or more columns.
-
Generate department-wise reports.
-
Perform grouped statistical analysis.
-
Apply summary functions in real-world business scenarios.
📖 2.33 Introduction to summarise()
Definition
The summarise() (or summarize()) function from the dplyr package is used to calculate summary statistics for a dataset. It reduces multiple rows into a single summary.
Common statistics include:
-
Mean
-
Sum
-
Minimum
-
Maximum
-
Count
-
Standard Deviation
-
Variance
Install and Load Package
📊 Sample Dataset (10 Records)
employee <- data.frame(
Emp_ID=c(101,102,103,104,105,106,107,108,109,110),
Name=c("Amit","Priya","Rahul","Sneha","Karan",
"Neha","Arjun","Pooja","Rohan","Anjali"),
Department=c("HR","Sales","IT","HR","IT",
"Finance","Sales","Finance","IT","HR"),
Age=c(25,28,30,27,35,31,29,33,26,32),
Salary=c(30000,35000,50000,32000,60000,
55000,40000,58000,45000,52000)
)
employee
Output
| Emp_ID | Name | Department | Age | Salary |
|---|
| 101 | Amit | HR | 25 | 30000 |
| 102 | Priya | Sales | 28 | 35000 |
| 103 | Rahul | IT | 30 | 50000 |
| 104 | Sneha | HR | 27 | 32000 |
| 105 | Karan | IT | 35 | 60000 |
| 106 | Neha | Finance | 31 | 55000 |
| 107 | Arjun | Sales | 29 | 40000 |
| 108 | Pooja | Finance | 33 | 58000 |
| 109 | Rohan | IT | 26 | 45000 |
| 110 | Anjali | HR | 32 | 52000 |
📖 2.34 Using summarise()
Syntax
💻 Example 1: Calculate Average Salary
Output
💻 Example 2: Total Salary
Output
💻 Example 3: Minimum and Maximum Salary
Output
💻 Example 4: Count Employees
Output
💻 Example 5: Standard Deviation
Output
| Standard_Deviation |
|---|
| 10682.07 (approx.) |
📖 2.35 Using group_by()
Definition
The group_by() function divides a dataset into groups. When used with summarise(), it calculates statistics for each group separately.
Syntax
💻 Example 6: Average Salary by Department
Output
| Department | Average Salary |
|---|
| Finance | 56500 |
| HR | 38000 |
| IT | 51667 |
| Sales | 37500 |
💻 Example 7: Total Salary by Department
Output
| Department | Total Salary |
|---|
| Finance | 113000 |
| HR | 114000 |
| IT | 155000 |
| Sales | 75000 |
💻 Example 8: Employee Count by Department
Output
| Department | Employees |
|---|
| Finance | 2 |
| HR | 3 |
| IT | 3 |
| Sales | 2 |
💻 Example 9: Department-wise Minimum and Maximum Salary
Output
| Department | Minimum | Maximum |
|---|
| Finance | 55000 | 58000 |
| HR | 30000 | 52000 |
| IT | 45000 | 60000 |
| Sales | 35000 | 40000 |
💻 Example 10: Multiple Summary Statistics
Output
| Department | Avg Age | Avg Salary | Highest | Lowest | Employees |
|---|
| Finance | 32.0 | 56500 | 58000 | 55000 | 2 |
| HR | 28.0 | 38000 | 52000 | 30000 | 3 |
| IT | 30.3 | 51667 | 60000 | 45000 | 3 |
| Sales | 28.5 | 37500 | 40000 | 35000 | 2 |
📊 Common Summary Functions
| Function | Purpose |
|---|
mean() | Average |
sum() | Total |
min() | Minimum |
max() | Maximum |
n() | Count |
sd() | Standard Deviation |
var() | Variance |
median() | Median |
📊 Comparison of Functions
| Function | Purpose |
|---|
summarise() | Creates summary statistics |
group_by() | Groups data into categories |
n() | Counts rows in each group |
mean() | Calculates average |
sum() | Calculates total |
🌍 Real-Life Applications
-
Department-wise salary analysis.
-
Student performance reports by class.
-
Monthly sales summaries by region.
-
Customer purchase analysis.
-
Banking transaction summaries.
-
Hospital patient statistics.
-
Inventory reports.
-
Business intelligence dashboards.
✔ Advantages
-
Produces concise statistical summaries.
-
Supports grouped analysis.
-
Easy to combine with other dplyr functions.
-
Ideal for dashboards and reports.
-
Highly efficient for large datasets.
✖ Limitations
-
Requires correctly grouped data.
-
Missing values should be handled before summarizing.
-
Complex summaries may require additional functions.
📝 Lab Exercises
-
Calculate the average salary of all employees.
-
Find the total salary paid.
-
Count the total number of employees.
-
Find the highest and lowest salary.
-
Calculate the standard deviation of salaries.
-
Find the average salary for each department.
-
Count employees in each department.
-
Calculate total salary by department.
-
Find the minimum and maximum salary for each department.
-
Create a department-wise summary showing average age, average salary, highest salary, lowest salary, and employee count.
❓ Viva Questions
-
What is the purpose of
summarise()?
-
What is the purpose of
group_by()?
-
Which function counts the number of rows?
-
How do you calculate the average salary?
-
What is the difference between
summarise() and group_by()?
-
Can
summarise() be used without group_by()?
-
Which function calculates standard deviation?
-
What is the purpose of
n()?
-
Why is grouped analysis important?
-
Give two real-life applications of
group_by().
Class 10 (Final): Complete Data Cleaning and Data Transformation Case Study
Duration: 1 Class
🎯 Learning Objectives
After completing this lesson, students will be able to:
-
Import data from a CSV file.
-
Explore the dataset.
-
Handle missing values.
-
Remove duplicate records.
-
Rename columns.
-
Transform data using dplyr.
-
Generate summary reports.
-
Export the processed dataset.
-
Apply the complete data analysis workflow in R.
📖 2.36 Complete Data Analysis Workflow
A typical data analysis project follows these steps:
📊 Case Study: Employee Salary Analysis
Suppose a company provides the following employee dataset.
Sample Dataset (10 Records)
employee <- data.frame(
Emp_ID = c(101,102,103,104,105,106,107,108,109,109),
Name = c("Amit","Priya","Rahul","Sneha","Karan",
"Neha","Arjun","Pooja","Rohan","Rohan"),
Department = c("HR","Sales","IT","HR","IT",
"Finance","Sales","Finance","IT","IT"),
Age = c(25,28,30,27,35,NA,29,33,26,26),
Salary = c(30000,35000,50000,32000,60000,
55000,40000,58000,45000,45000)
)
employee
Output
| Emp_ID | Name | Department | Age | Salary |
|---|
| 101 | Amit | HR | 25 | 30000 |
| 102 | Priya | Sales | 28 | 35000 |
| 103 | Rahul | IT | 30 | 50000 |
| 104 | Sneha | HR | 27 | 32000 |
| 105 | Karan | IT | 35 | 60000 |
| 106 | Neha | Finance | NA | 55000 |
| 107 | Arjun | Sales | 29 | 40000 |
| 108 | Pooja | Finance | 33 | 58000 |
| 109 | Rohan | IT | 26 | 45000 |
| 109 | Rohan | IT | 26 | 45000 |
Notice that:
-
One missing value exists in Age.
-
One duplicate employee record exists.
Step 1: Explore the Dataset
Program 1
Output
Step 2: Detect Missing Values
Program 2
Output
Step 3: Replace Missing Age with Mean
Program 3
Output
Step 4: Detect Duplicate Records
Program 4
Output
Step 5: Remove Duplicate Records
Program 5
Output
Step 6: Rename Columns
Program 6
Output
Step 7: Create Bonus Column
Program 7
Output
| Employee_Name | Salary | Bonus |
|---|
| Amit | 30000 | 3000 |
| Priya | 35000 | 3500 |
| Rahul | 50000 | 5000 |
| Sneha | 32000 | 3200 |
| Karan | 60000 | 6000 |
| Neha | 55000 | 5500 |
| Arjun | 40000 | 4000 |
| Pooja | 58000 | 5800 |
| Rohan | 45000 | 4500 |
Step 8: Create Gross Salary
Program 8
Output
| Employee_Name | Gross Salary |
|---|
| Amit | 33000 |
| Priya | 38500 |
| Rahul | 55000 |
| Sneha | 35200 |
| Karan | 66000 |
| Neha | 60500 |
| Arjun | 44000 |
| Pooja | 63800 |
| Rohan | 49500 |
Step 9: Department-wise Summary
Program 9
Output
| Department | Employees | Average Salary | Highest | Lowest |
|---|
| Finance | 2 | 56500 | 58000 | 55000 |
| HR | 2 | 31000 | 32000 | 30000 |
| IT | 3 | 51667 | 60000 | 45000 |
| Sales | 2 | 37500 | 40000 | 35000 |
Step 10: Export Processed Dataset
Program 10
Output
📊 Complete Workflow Summary
| Step | Function |
|---|
| Import Data | read.csv() |
| Check Structure | str() |
| Summary | summary() |
| Missing Values | is.na() |
| Remove Missing | na.omit() |
| Replace Missing | mean() |
| Duplicate Detection | duplicated() |
| Remove Duplicates | !duplicated() |
| Rename Columns | colnames() |
| Create New Columns | mutate() |
| Group Data | group_by() |
| Statistical Summary | summarise() |
| Export Data | write.csv() |
📊 Best Practices
✔ Keep a backup of the original dataset.
✔ Handle missing values before analysis.
✔ Remove duplicate records carefully.
✔ Use meaningful column names.
✔ Verify data types.
✔ Use group_by() for grouped analysis.
✔ Export the final cleaned dataset.
✔ Document every transformation step.
⚠ Common Errors and Solutions
| Error | Cause | Solution |
|---|
| Object not found | Incorrect variable name | Check spelling |
| Missing package | Package not installed | install.packages() |
| NA values in mean | Missing values present | Use na.rm = TRUE |
| Duplicate records | Repeated data | Use duplicated() |
| Wrong column name | Typing mistake | Use colnames() |
🌍 Real-Life Applications
-
Employee payroll processing.
-
Student examination systems.
-
Banking customer databases.
-
Hospital patient records.
-
Insurance claim processing.
-
Retail sales analysis.
-
Inventory management.
-
Government census data.
-
Customer relationship management (CRM).
-
Machine learning data preprocessing.
📝 Lab Programs
-
Import a CSV file.
-
Display the first 10 records.
-
Check the structure of the dataset.
-
Count missing values.
-
Replace missing values with the mean.
-
Detect duplicate records.
-
Remove duplicate records.
-
Rename all columns.
-
Create a Bonus column.
-
Calculate Gross Salary.
-
Calculate Annual Salary.
-
Group employees by department.
-
Calculate average salary department-wise.
-
Export the cleaned dataset.
-
Create a complete employee report.
❓ Viva Questions
-
What is data cleaning?
-
What is data transformation?
-
Which function imports a CSV file?
-
How do you detect missing values?
-
Which function removes duplicate records?
-
What is the purpose of
mutate()?
-
What is
group_by() used for?
-
Which function exports data to CSV?
-
Why is data cleaning important?
-
What are the steps in a data analysis workflow?
-
What is the difference between
summarise() and mutate()?
-
Why are meaningful column names important?
-
What is the purpose of
na.rm = TRUE?
-
How do you calculate department-wise statistics?
-
Give three real-life applications of data transformation.
-
What is the use of
duplicated()?
-
How do you create a new variable in R?
-
What is the difference between CSV and Excel files?
-
Why should raw data be backed up before cleaning?
-
Explain the complete data analysis process in R.
📚 Module 2 Summary
In this module, you learned:
-
Importing and exporting data using CSV and Excel files.
-
Handling missing values and duplicate records.
-
Converting data types.
-
Renaming rows and columns.
-
Selecting, filtering, and arranging data.
-
Creating and transforming variables with
mutate() and transmute().
-
Summarizing data using
summarise() and group_by().
-
Applying a complete data cleaning and transformation workflow using R.
-
Solving real-world data analysis problems with practical R programs and outputs.
Module 3: Data Visualization in R Programming
📘 CHAPTER 1: Introduction to Data Visualization in R
🌟 1.1 What is Data Visualization?
Data Visualization is the graphical representation of data using charts, graphs, and plots.
It helps to convert raw data into meaningful visual information.
🎯 Purpose:
-
To understand patterns in data
-
To identify trends and relationships
-
To detect outliers
-
To support decision making
📊 1.2 Importance of Data Visualization
-
Makes complex data easy to understand
-
Improves analysis speed
-
Helps in statistical interpretation
-
Useful in business intelligence
-
Enhances presentation quality
📈 1.3 Types of Data Visualizations in R
| Type | Purpose |
|---|
| Scatter Plot | Relationship between variables |
| Line Plot | Trend analysis |
| Bar Chart | Category comparison |
| Histogram | Data distribution |
| Pie Chart | Percentage representation |
| Box Plot | Outlier detection |
🟦 1.4 Base R Graphics
Base R provides built-in functions to create plots without installing additional packages.
🔧 Common Functions:
-
plot() → General plotting
-
barplot() → Bar chart
-
hist() → Histogram
-
pie() → Pie chart
-
boxplot() → Box plot
📍 1.5 Scatter Plot in Base R
🎯 Objective:
To show relationship between two variables.
💻 R Script:
🖥️ Output:
-
A blue scatter plot
-
Points increasing diagonally
-
Title: Scatter Plot Example
📌 Interpretation:
There is a positive relationship between X and Y values.
📉 1.6 Line Plot in Base R
💻 R Script:
🖥️ Output:
-
Red line graph
-
Shows increasing trend
📌 Interpretation:
Sales are increasing steadily over time.
📊 1.7 Bar Plot in Base R
💻 R Script:
🖥️ Output:
-
Green vertical bars
-
Categories A, B, C, D
📊 1.8 Histogram in Base R
💻 R Script:
🖥️ Output:
-
Blue histogram bars
-
Frequency distribution of marks
🥧 1.9 Pie Chart in Base R
💻 R Script:
🖥️ Output:
-
Multicolor pie chart
-
Shows percentage distribution
📦 1.10 Box Plot in Base R
💻 R Script:
🖥️ Output:
-
Orange box plot
-
Shows median and spread
⚡ 1.11 Key Advantages of Base R Graphics
-
Easy to use
-
No installation required
-
Fast execution
-
Good for basic analysis
📌 1.12 Summary
-
Data visualization converts data into graphical form
-
Base R provides simple plotting tools
-
Common plots: scatter, line, bar, histogram, pie, box
-
Helps in understanding patterns and trends
❓ 1.13 Viva Questions
-
What is data visualization?
-
What is the use of plot() in R?
-
What is a scatter plot?
-
Difference between bar plot and histogram?
-
What is the purpose of a box plot?
-
What does col parameter do?
-
What is the use of pch in scatter plot?
📘 CHAPTER 2: Advanced Data Visualization Using Base R Graphics + Introduction to ggplot2
🌟 2.1 Limitations of Base R Graphics
Although Base R graphics are useful, they have some limitations:
-
❌ Limited customization
-
❌ Not visually attractive for reports
-
❌ Difficult to create complex plots
-
❌ No grammar-based structure
-
❌ Hard to build advanced dashboards
👉 To overcome these problems, we use ggplot2
🎨 2.2 Introduction to ggplot2
ggplot2 is a powerful visualization package in R based on the Grammar of Graphics.
📦 Install Package:
📥 Load Package:
📚 2.3 Grammar of Graphics (Core Concept)
A plot in ggplot2 is built using layers:
🧩 Components:
| Component | Meaning |
|---|
| Data | Dataset |
| Aesthetics (aes) | Mapping variables |
| Geom | Type of plot |
| Stats | Statistical transformation |
| Coord | Coordinate system |
| Theme | Visual appearance |
📊 2.4 Basic ggplot Structure
📌 2.5 Example Dataset
📍 2.6 Scatter Plot (ggplot2)
🖥️ Output:
-
Blue circular points
-
Clear relationship between Age and Marks
📉 2.7 Line Plot (ggplot2)
🖥️ Output:
-
Red line connecting points
-
Black dots on each value
📊 2.8 Bar Plot (ggplot2)
🖥️ Output:
-
Green vertical bars
-
Each student’s marks compared
📊 2.9 Histogram (ggplot2)
🖥️ Output:
-
Histogram showing frequency of marks
📦 2.10 Box Plot (ggplot2)
🖥️ Output:
-
Orange box showing median & outliers
🌈 2.11 Density Plot
🖥️ Output:
-
Smooth curve showing distribution
🎨 2.12 Customizing ggplot2
🔹 Titles & Labels
🔹 Themes
Other Themes:
-
theme_bw()
-
theme_classic()
-
theme_dark()
🔹 Colors & Size
🔹 Scales
🧩 2.13 Faceting (Multiple Plots)
🖥️ Output:
-
Separate plots for Male and Female
📊 2.14 Multiple Plot Layout
📌 2.15 Summary
-
Base R is simple but limited
-
ggplot2 is powerful and flexible
-
Grammar of Graphics is core concept
-
Customization is easy in ggplot2
-
Faceting helps in multi-view analysis
❓ 2.16 Viva Questions
-
What is ggplot2?
-
What is Grammar of Graphics?
-
Difference between base R and ggplot2?
-
What is aes() in ggplot2?
-
What is geom_point()?
-
What is faceting?
-
What is theme in ggplot2?
-
What is density plot?
📘 CHAPTER 3: Interactive Data Visualization in R (Plotly & Shiny)
🌟 3.1 What is Interactive Visualization?
Interactive visualization allows users to:
-
🔍 Zoom in/out of graphs
-
🖱️ Hover to see values
-
🎯 Click and explore data
-
📊 Filter and analyze dynamically
👉 It makes data exploration more powerful than static graphs.
📦 3.2 Plotly in R
Plotly is used to create interactive charts in R.
📥 Install Plotly
📥 Load Library
📊 3.3 Interactive Scatter Plot
🖥️ Output:
-
Interactive blue points
-
Hover shows values
-
Zoom enabled
📈 3.4 Interactive Line Plot
🖥️ Output:
-
Red curve showing quadratic growth
-
Click and zoom enabled
📊 3.5 Interactive Bar Chart
🖥️ Output:
-
Green bars
-
Hover shows values
📊 3.6 ggplot2 + Plotly Integration
🖥️ Output:
-
Interactive bar chart
-
Hover + zoom + click enabled
🌐 3.7 Introduction to Shiny
Shiny is used to create interactive web applications in R.
👉 Used for:
-
Dashboards
-
Data apps
-
Live reports
📥 Install Shiny
📥 Load Library
🧱 3.8 Structure of Shiny App
A Shiny app has 2 parts:
| Component | Purpose |
|---|
| UI | User Interface |
| Server | Logic/Backend |
📱 3.9 Simple Shiny App
🖥️ Output:
-
Slider input (1–100)
-
Dynamic text updates instantly
📊 3.10 Shiny Dashboard Example
🖥️ Output:
-
Dropdown menu
-
Dynamic response display
📊 3.11 Advantages of Interactive Visualization
-
🎯 Real-time interaction
-
📊 Better data understanding
-
📈 Professional dashboards
-
🧠 Easy decision-making
-
🌐 Web-based applications
⚖️ 3.12 Comparison
| Tool | Type | Use |
|---|
| Base R | Static | Basic plots |
| ggplot2 | Static advanced | Publication graphs |
| Plotly | Interactive | Dynamic charts |
| Shiny | Web app | Dashboards |
📌 3.13 Summary
-
Plotly adds interactivity to graphs
-
ggplotly converts ggplot to interactive charts
-
Shiny creates full web applications
-
Interactive tools are used in real-world analytics
❓ 3.14 Viva Questions
-
What is interactive visualization?
-
What is Plotly used for?
-
What is Shiny in R?
-
Difference between ggplot2 and Plotly?
-
What are UI and Server in Shiny?
-
What is ggplotly()?
-
What are dashboards?
🎓 FINAL SUMMARY (FULL MODULE)
✔ Base R Graphics → Simple plots
✔ ggplot2 → Advanced visualization
✔ Plotly → Interactive charts
✔ Shiny → Full web dashboards
📘 MODULE 4: STATISTICAL ANALYSIS AND MODELING
Class 1: Descriptive Statistics and Measures of Central Tendency
🌟 Learning Objectives
After completing this chapter, students will be able to:
- Understand the concept of descriptive statistics.
- Explain measures of central tendency.
- Calculate Mean, Median, and Mode using R.
- Interpret statistical results.
- Apply descriptive statistics to real-world data.
📚 4.1 Introduction to Statistics
Statistics is the science of collecting, organizing, analyzing, interpreting, and presenting data. It helps researchers, businesses, scientists, and governments make informed decisions based on numerical information.
For example:
- A school calculates the average marks of students.
- A company analyzes monthly sales.
- A hospital studies patient recovery rates.
- Weather departments analyze temperature records.
Statistics transforms raw data into useful information.
📖 Types of Statistics
Statistics is broadly classified into two categories:
1. Descriptive Statistics
Descriptive statistics summarizes and describes the main features of a dataset. It does not make predictions but presents the data in a meaningful way.
Examples:
- Mean
- Median
- Mode
- Range
- Variance
- Standard Deviation
Applications
- Student result analysis
- Employee salary reports
- Sales reports
- Population surveys
2. Inferential Statistics
Inferential statistics uses sample data to make predictions or conclusions about a larger population.
Examples:
- Hypothesis testing
- Regression analysis
- ANOVA
- Confidence intervals
⭐ Importance of Descriptive Statistics
Descriptive statistics helps to:
- Summarize large datasets.
- Identify patterns and trends.
- Compare different datasets.
- Support decision-making.
- Prepare data for advanced analysis.
📊 Measures of Central Tendency
Measures of Central Tendency describe the center or typical value of a dataset.
The three common measures are:
- Mean
- Median
- Mode
🔵 4.2 Mean (Arithmetic Mean)
Definition
The Mean is the arithmetic average of all observations.
It is the most commonly used measure of central tendency.
Formula
Mean=N∑X
Where:
- ΣX = Sum of all observations
- N = Number of observations
Sample Data (10 Students' Marks)
| Student | Marks |
|---|
| 1 | 45 |
| 2 | 52 |
| 3 | 58 |
| 4 | 63 |
| 5 | 67 |
| 6 | 72 |
| 7 | 78 |
| 8 | 84 |
| 9 | 90 |
| 10 | 95 |
Manual Calculation
Step 1: Add all values
45 + 52 + 58 + 63 + 67 + 72 + 78 + 84 + 90 + 95
= 704
Step 2: Count observations
Number of observations = 10
Step 3: Apply Formula
Mean = 704 ÷ 10
= 70.4
💻 R Program
🖥 Output
📖 Explanation
The mean() function in R calculates the arithmetic average of all values in the vector.
returns
because the total marks are 704, divided by 10 students.
✅ Interpretation
The average marks obtained by the students are 70.4.
This means that if the total marks were equally distributed among all students, each student would receive 70.4 marks.
🌍 Real-Life Applications of Mean
- Calculating students' average marks.
- Measuring average monthly income.
- Determining average rainfall.
- Calculating average temperature.
- Business profit analysis.
- Cricket batting average.
- Manufacturing quality control.
✔ Advantages of Mean
- Easy to calculate.
- Uses all observations.
- Suitable for mathematical analysis.
- Widely used in statistics.
✖ Disadvantages of Mean
- Affected by very high or very low values (outliers).
- Not suitable for highly skewed data.
- Cannot be used for categorical data.
💡 Important Note
The Mean is the most widely used measure of central tendency, but it can be misleading when a dataset contains extreme values.
📝 Practice Exercise
Use the following data to calculate the Mean manually and using R.
Write an R Program
Expected Output
📌 Key Points
- Mean is the arithmetic average.
- It is calculated using all observations.
- R provides the
mean() function. - Mean is affected by extreme values.
- It is widely used in business, science, education, and research.
🎯 Learning Summary
After completing this lesson, you have learned:
- What is descriptive statistics?
- Types of statistics.
- Importance of descriptive statistics.
- Definition and formula of Mean.
- Manual calculation of Mean.
- R program to calculate Mean.
- Interpretation of output.
- Applications, advantages, and disadvantages of Mean.
🔴 4.3 Median
📖 Definition
The Median is the middle value of a dataset when the observations are arranged in ascending or descending order.
Unlike the Mean, the Median is not affected by extremely high or low values (outliers). Therefore, it is considered a better measure of central tendency for skewed data.
🎯 Formula
For Odd Number of Observations
Median=(2n+1)th Observation
For Even Number of Observations
Median=2Middle Value1+Middle Value2
Where:
- n = Total number of observations
📊 Example (10 Student Marks)
| Student | Marks |
|---|
| 1 | 45 |
| 2 | 52 |
| 3 | 58 |
| 4 | 63 |
| 5 | 67 |
| 6 | 72 |
| 7 | 78 |
| 8 | 84 |
| 9 | 90 |
| 10 | 95 |
The data is already arranged in ascending order.
🧮 Manual Calculation
Number of observations = 10 (Even)
Middle positions:
- 5th value = 67
- 6th value = 72
Median
= (67 + 72) ÷ 2
= 69.5
💻 R Program
🖥 Output
📖 Explanation
The median() function automatically sorts the values (if required) and finds the middle value.
For an even number of observations, it calculates the average of the two middle values.
✅ Interpretation
The median marks are 69.5.
This means:
- 50% of students scored below 69.5
- 50% of students scored above 69.5
🌍 Real-Life Applications
- Income analysis
- House price analysis
- Population studies
- Salary surveys
- Medical research
✔ Advantages
- Not affected by outliers.
- Easy to understand.
- Suitable for skewed data.
- Useful for ordinal data.
✖ Disadvantages
- Does not use every observation.
- Difficult to calculate for grouped data manually.
📝 Practice Exercise
Find the median of the following data using R.
Sample Data
28, 35, 40, 45, 50, 55, 60, 65, 70, 80
R Script
Output
🟣 4.4 Mode
📖 Definition
The Mode is the value that appears most frequently in a dataset.
A dataset may have:
- One Mode (Unimodal)
- Two Modes (Bimodal)
- More than Two Modes (Multimodal)
- No Mode (all values occur once)
Since R does not provide a built-in function for statistical mode, we create a custom function.
📊 Example (10 Student Marks)
| Student | Marks |
|---|
| 1 | 45 |
| 2 | 52 |
| 3 | 63 |
| 4 | 63 |
| 5 | 63 |
| 6 | 72 |
| 7 | 78 |
| 8 | 84 |
| 9 | 90 |
| 10 | 95 |
📋 Frequency Table
| Marks | Frequency |
|---|
| 45 | 1 |
| 52 | 1 |
| 63 | 3 |
| 72 | 1 |
| 78 | 1 |
| 84 | 1 |
| 90 | 1 |
| 95 | 1 |
The highest frequency is 3.
Therefore,
Mode = 63
💻 R Program
🖥 Output
📖 Explanation
The custom Mode() function:
- Finds the unique values.
- Counts how many times each value appears.
- Returns the value with the highest frequency.
✅ Interpretation
The most frequently occurring mark is 63.
This indicates that 63 is the most common score among the students.
🌍 Real-Life Applications
- Most sold product
- Most common blood group
- Most frequently purchased item
- Customer preference analysis
- Election survey analysis
✔ Advantages
- Easy to understand.
- Suitable for categorical data.
- Not affected by outliers.
- Represents the most common value.
✖ Disadvantages
- Some datasets have multiple modes.
- Some datasets have no mode.
- Less useful for mathematical calculations.
📊 Comparison of Mean, Median, and Mode
| Feature | Mean | Median | Mode |
|---|
| Definition | Average of all values | Middle value | Most frequent value |
| Uses All Data | ✔ Yes | ✖ No | ✖ No |
| Affected by Outliers | ✔ Yes | ✖ No | ✖ No |
| Suitable for Categorical Data | ✖ No | ✖ No | ✔ Yes |
| R Function | mean() | median() | Custom Function |