R Programming Tutorial

1. Getting Started with R Programming
1. Introduction to R and Its Applications

What is R?

  • R is a programming language and software environment used for statistical computing, data analysis, and visualization.
  • It is open-source and widely used by statisticians, data scientists, and researchers.

Applications of R:

  • Statistical Analysis
  • Data Visualization: using libraries like ggplot2 and plotly
  • Machine Learning
  • Data Cleaning
  • Bioinformatics
  • Time Series Analysis
2. Installing R and R Studio

Steps to Install R:

  1. Visit the CRAN website.
  2. Select your operating system (Windows, macOS, or Linux).
  3. Download and install R.

Steps to Install R Studio:

  1. Go to the R Studio website.
  2. Download and install R Studio.

Tip: Install R before R Studio.

3. Overview of R Studio Interface
  • Source Pane: Script editor with syntax highlighting.
  • Console: Execute commands interactively.
  • Environment/History: View variables and past commands.
  • Files/Plots/Packages/Help: Tools and documentation.

Customize R Studio under Tools > Global Options > Appearance.

4. Writing Your First R Script

Open R Studio and go to File > New File > R Script and write:

# This is a comment
print("Hello, World!")
x <- 5
y <- 10
z <- x + y
print(z)

Output:

[1] "Hello, World!"
[1] 15
5. Basic Commands in R Console

Math Operations:

5 + 3
5 - 3
5 * 3
5 / 3
5^3
sqrt(25)

Variable Assignment:

x <- 10
y = 20
z <- x + y
print(z)

Logical Operators:

5 > 3
5 == 3
5 != 3

Data Types:

class(x)
is.character("text")
is.numeric(10)

Vectors:

v <- c(1, 2, 3, 4, 5)
sum(v)
length(v)
mean(v)

Help Functions:

?mean
help("plot")
2. Learn Variables in R Programming
1. What Are Variables?

A variable is like a container that stores data. In R, variables are used to store values, such as numbers, text, or more complex data like lists and data frames.

x <- 10  # Here, 'x' is the variable storing the value 10.

Analogy: Think of variables like labeled jars where you store different types of ingredients.

2. Assigning Data to Variables in R

R uses assignment operators like <- and =.

# Using <- for assignment
my_number <- 42
my_text <- "Hello, World!"
my_vector <- c(1, 2, 3, 4, 5)

# Using = for assignment
another_number = 99

Tip: Use <- for clarity and consistency in R scripts.

3. Rules for Naming Variables
  • Must start with a letter.
  • Can contain letters, numbers, underscores, or dots.
  • No spaces or hyphens.
  • Case-sensitive.
valid_name <- 10
another_valid_name <- "R programming"
# Invalid examples:
# 1invalid <- 5
# my-var <- 20
4. Best Practices
  • Use meaningful names like total_sales.
  • Avoid names like mean or sum.
  • Stick to naming styles: snake_case or camelCase.
5. Practical Exercises
Exercise 1: Assign value 50 to a variable my_age and create a vector:
my_age <- 50
my_numbers <- c(5, 10, 15, 20)
Exercise 2: Modify value:
price <- 100
price <- price + 50  # Now, price is 150
6. Interactive Quiz

Q1: What is the value of z?

x <- 10
y <- x * 2
z <- y + 5

Answer: 25

Q2: Is total.sales valid?

Answer: Yes

7. Challenge Section

Create a vector prices <- c(120, 150, 100, 180). Add 20% tax and store total:

prices <- c(120, 150, 100, 180)
prices_with_tax <- prices * 1.2
total_price <- sum(prices_with_tax)
3. Understanding Vectors
1. Definition:

A vector is the simplest data structure in R. It contains elements of the same type: numeric, character, or logical.

2. How to Create a Vector:

Use the c() function to combine values into a vector.

3. Examples:
# Numeric vector
numeric_vector <- c(1, 2, 3, 4, 5)

# Character vector
char_vector <- c("apple", "banana", "cherry")

# Logical vector
logical_vector <- c(TRUE, FALSE, TRUE)
4. Accessing Elements:
print(numeric_vector[1])  # Access the first element
5. Operations on Vectors:
sum(numeric_vector)   # Calculate sum
mean(numeric_vector)  # Calculate mean
Key Features of Vectors
  • Homogeneous Data Structure: All elements must be of the same type.
  • Indexed Elements: Indexing starts from 1.
  • Vectorized Operations: Supports fast, efficient element-wise operations.
Creating Vectors

Using the c() Function:

numeric_vector <- c(10, 20, 30)
char_vector <- c("dog", "cat", "rabbit")

Using seq() Function:

seq_vector <- seq(1, 10)
step_vector <- seq(1, 10, by = 2)

Using rep() Function:

repeat_vector <- rep(5, times = 3)
repeat_sequence <- rep(c(1, 2), each = 3)
Accessing and Modifying Vector Elements
numeric_vector <- c(100, 200, 300)
print(numeric_vector[1])
print(numeric_vector[c(1, 3)])
numeric_vector[2] <- 250
print(numeric_vector)
greater_than_100 <- numeric_vector[numeric_vector > 100]
print(greater_than_100)
Vectorized Operations
vector1 <- c(2, 4, 6)
vector2 <- c(1, 3, 5)
sum_vector <- vector1 + vector2
product_vector <- vector1 * vector2
print(sum_vector)
print(product_vector)

sum_result <- sum(vector1)
mean_result <- mean(vector1)
length_result <- length(vector1)
print(c(sum_result, mean_result, length_result))

comparison <- vector1 > vector2
print(comparison)
Practical Example

Problem: Create a numeric vector for marks in 5 subjects and perform calculations.

marks <- c(85, 90, 78, 65, 88)
total_marks <- sum(marks)
print(total_marks)

average_marks <- mean(marks)
print(average_marks)

pass_status <- marks >= 40
print(pass_status)

Output:

[1] 406
[1] 81.2
[1] TRUE TRUE TRUE TRUE TRUE
Exercises for Students
  • Create a numeric vector of your five favorite numbers.
  • Create a character vector with your three favorite fruits.
  • Create a logical vector for values > 10 from c(5, 12, 18, 7).
  • Access 2nd and 4th elements of c(3, 8, 15, 22, 9).
  • Replace 3rd element of c(10, 20, 30, 40, 50) with 35.
  • Filter values > 25 from c(10, 25, 30, 40).
  • Add c(2, 4, 6, 8) and c(1, 3, 5, 7).
  • Multiply elements of c(3, 6, 9).
  • Mean of c(20, 25, 30, 35, 40).
  • Compare c(10, 15, 20) with c(12, 15, 18).
Challenge Exercise
  1. Create monthly expenses: c(2000, 2500, 1800, 2200, 2700, 3000).
  2. Calculate total expenses over 6 months.
  3. Identify months where expenses > 2500.
  4. Reduce expenses by 10% and display updated values.
Conclusion

Vectors are fundamental in R programming and essential for almost every type of analysis. Understanding how to create, access, and operate on vectors efficiently is key for any R programmer.

4. Understanding Matrices in R
1. Definition

A matrix in R is a two-dimensional data structure where all elements must be of the same data type (e.g., numeric, character, or logical). Matrices are used for mathematical operations, storing tabular data, and more.

2. How to Create a Matrix

Using the matrix() Function:

matrix_example <- matrix(1:9, nrow=3, ncol=3)
print(matrix_example)
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

Using Vectors:

vector <- c(10, 20, 30, 40, 50, 60)
matrix_from_vector <- matrix(vector, nrow=2, ncol=3)
print(matrix_from_vector)
3. Accessing Matrix Elements
print(matrix_example[2, 3])  # 2nd row, 3rd column
print(matrix_example[2, ])   # Entire 2nd row
print(matrix_example[, 3])   # Entire 3rd column

matrix_example[1, 1] <- 99
print(matrix_example)
4. Operations on Matrices
# Add matrices
matrix1 <- matrix(1:4, nrow=2)
matrix2 <- matrix(5:8, nrow=2)
result <- matrix1 + matrix2

# Row/Column sums
rowSums(matrix_example)
colSums(matrix_example)

# Multiplication
product <- matrix1 %*% matrix2

# Transpose
transpose <- t(matrix_example)
5. Real-Life Example
sales <- matrix(
  c(100, 200, 150, 250, 300, 100, 200, 150, 400, 300, 200, 100),
  nrow=3, ncol=4, byrow=TRUE
)

rownames(sales) <- c("Product_A", "Product_B", "Product_C")
colnames(sales) <- c("Jan", "Feb", "Mar", "Apr")
print(sales)

rowSums(sales)
colSums(sales)
6. Exercises for Students
  • Create a 3x3 matrix and access [2,3].
  • Create 2x4 matrix with 10 to 80.
  • Add two 3x3 matrices.
  • Find row/column sums of:
    matrix(c(5,10,...,45), nrow=3)
  • Store exam scores for 4 students × 5 subjects and find averages.
  • Transpose a 3x3 matrix and verify double transpose.
  • Multiply:
    matrix1 <- matrix(c(1,2,3,4), nrow=2)
    matrix2 <- matrix(c(5,6,7,8), nrow=2)
7. Conclusion

Matrices are vital in R for data manipulation and operations. Practice regularly to build strong R skills!

5. Learn Data Frames in R Programming
What is a Data Frame?

A data frame is a two-dimensional, table-like structure where:

  • Rows represent observations.
  • Columns represent variables.
  • Each column can have a different data type (numeric, character, factor, etc.).
Why Use Data Frames?

Data frames are the primary way to store and manipulate structured data in R. They are widely used for:

  1. Data analysis and exploration.
  2. Data cleaning and transformation.
  3. Integration with various machine learning and visualization tools.
1. Creating a Data Frame
# Create vectors
names <- c("Alice", "Bob", "Charlie", "David")
ages <- c(25, 30, 35, 40)
salaries <- c(50000, 60000, 70000, 80000)
is_manager <- c(TRUE, FALSE, TRUE, FALSE)

# Combine into a data frame
employees <- data.frame(Name = names, Age = ages, Salary = salaries, Manager = is_manager)

# Print the data frame
print(employees)

Output:

     Name Age Salary Manager
1   Alice  25  50000    TRUE
2     Bob  30  60000   FALSE
3 Charlie  35  70000    TRUE
4   David  40  80000   FALSE
2. Exploring Data Frames

Check structure and summary:

# Check structure
str(employees)

# View summary
summary(employees)

Access specific rows and columns:

# Access a specific column
print(employees$Name)

# Access a specific row
print(employees[1, ])

# Access multiple rows and columns
print(employees[1:2, c("Name", "Salary")])
3. Modifying a Data Frame
# Add a new column for bonuses
employees$Bonus <- employees$Salary * 0.1
print(employees)

# Update salary with a 10% increment
employees$Salary <- employees$Salary * 1.1
print(employees)

# Remove the Bonus column
employees$Bonus <- NULL
print(employees)
4. Filtering Data
# Filter managers only
managers <- employees[employees$Manager == TRUE, ]
print(managers)

# Filter employees older than 30 and earning more than 60,000
filtered <- employees[employees$Age > 30 & employees$Salary > 60000, ]
print(filtered)
5. Sorting a Data Frame
# Sort by Age
sorted_by_age <- employees[order(employees$Age), ]

# Sort by Salary (descending)
sorted_by_salary <- employees[order(-employees$Salary), ]
6. Importing and Exporting Data Frames
# Read from a CSV file
employees <- read.csv("employees.csv")

# Write to a CSV file
write.csv(employees, "updated_employees.csv", row.names = FALSE)
Practice Exercises

1. Create a Data Frame:

  • Create a data frame with student names, their scores in Math, Science, and English, and their grades (A, B, C, etc.).
  • Add a new column for the average score.
  • Filter students with an average score above 75.

2. Manipulate Data (mtcars):

  • Extract rows where mpg > 20 and cyl == 6.
  • Sort the dataset by hp in descending order.
  • Add a column categorizing mpg into "High", "Medium", and "Low."

3. Load and Save Data:

  • Save the modified mtcars dataset into a CSV file.
  • Read it back into R and confirm the data integrity.
Recap
  • What a data frame is and why it's useful.
  • Creating and exploring data frames.
  • Modifying, filtering, and sorting data.
  • Importing and exporting data frames.
Code for Practice Exercises
# Example: Student Scores Data Frame
students <- data.frame(
  Name = c("John", "Sarah", "Tom", "Emily"),
  Math = c(80, 90, 70, 85),
  Science = c(75, 95, 65, 80),
  English = c(88, 92, 78, 89),
  Grade = c("B", "A", "C", "B")
)

# Add average score column
students$Average <- rowMeans(students[, c("Math", "Science", "English")])

# Filter students with average score > 75
top_students <- students[students$Average > 75, ]
print(top_students)
6. Learn "if" statement in R Programming
1. `if` Statement

The if statement is used to execute code when a condition is true.

Syntax:
if (condition) {
  # Code to execute if condition is TRUE
}
Example:
x <- 10
if (x > 5) {
  print("x is greater than 5")
}

EXAMPLES FOR IF:

Here are some advanced examples of if statements in R to make your YouTube video more engaging and informative:

2. Check for Even or Divisibility Condition

Use an if statement to verify if a number is even and divisible by another number.

Example:
x <- 20
if (x %% 2 == 0 && x %% 5 == 0) {
  print(paste(x, "is even and divisible by 5"))
}
Breaking down the function:

Let’s break down the provided R function into its individual components and explain what each part means:


Code:
x <- 20
if (x %% 2 == 0 && x %% 5 == 0) {
  print(paste(x, "is even and divisible by 5"))
}

Step-by-Step Explanation
1. Variable Assignment
x <- 20
  • x: A variable that stores the value 20.
  • <-: The assignment operator.

2. `if` Statement
if (x %% 2 == 0 && x %% 5 == 0)
  • x %% 2 == 0: Checks if x is divisible by 2.
  • &&: Logical AND operator.
  • x %% 5 == 0: Checks if x is divisible by 5.

3. Code Block
{
  print(paste(x, "is even and divisible by 5"))
}

This block executes only if the if condition is true.

3. Check for the Existence of a Value in a Vector

Use if to determine if a specific value exists in a vector.

Example:
vec <- c(10, 20, 30, 40)
val <- 25

if (val %in% vec) {
  print(paste(val, "exists in the vector"))
}
Explanation:

Vector Creation: vec <- c(10, 20, 30, 40)

Value Assignment: val <- 25

The if statement uses the operator %in% to check if val exists in vec. Since 25 is not in vec, the condition evaluates to FALSE and nothing is printed.

4. Conditional Execution with Nested `if`
Example:
x <- 45

if (x > 10) {
  if (x %% 5 == 0) {
    print(paste(x, "is greater than 10 and divisible by 5"))
  }
}
5. Compare Two Numeric Variables
Example:
a <- 25
b <- 30

if (a < b) {
  print(paste(a, "is less than", b))
}
6. Check for a Specific Condition in a Data Frame
Example:
data <- data.frame(name = c("Alice", "Bob", "Charlie"), score = c(85, 40, 65))

if ("Alice" %in% data$name) {
  print("Alice is in the data frame")
}
7. Execute Code Based on String Matching
Example:
text <- "R programming is fun"

if (grepl("programming", text)) {
  print("The word 'programming' is found in the text")
}
Code Details:
text <- "R programming is fun"

if (grepl("programming", text)) {
  print("The word 'programming' is found in the text")
}
7. grepl() in R Programming
1. What is grepl()?

The grepl() function stands for "grep logical". It searches for a pattern in a character vector and returns a logical vector (TRUE or FALSE) indicating whether the pattern is found in each element.

Lets understand the Syntax:
grepl(pattern, x, ignore.case = FALSE, fixed = FALSE)
  • pattern: The text or regular expression to search for.
  • x: The character vector to search in.
  • ignore.case: If TRUE, the search is case-insensitive (default is FALSE).
  • fixed: If TRUE, the pattern is treated as a literal string, not a regular expression.
2. Simple Examples of grepl()
Example 1: Check for a Word in a Vector
# Define a character vector
text_vector <- c("apple", "banana", "cherry", "date")

# Search for "apple"
result <- grepl("apple", text_vector)
print(result)

Output:

[1]  TRUE FALSE FALSE FALSE

Here, TRUE indicates that "apple" is found in the first element.

Example 2: Case-Insensitive Search
# Case-insensitive search for "APPLE"
result <- grepl("APPLE", text_vector, ignore.case = TRUE)
print(result)

Output:

[1]  TRUE FALSE FALSE FALSE
Example 3: Use Regular Expressions
# Search for words starting with "b"
result <- grepl("^b", text_vector)
print(result)

Output:

[1] FALSE  TRUE FALSE FALSE

The ^b pattern matches strings starting with "b".

3. Real-World Dataset Example
Step 1: Generate the Dataset
# Create a mock dataset of customer reviews
reviews <- data.frame(
  CustomerID = 1:10,
  ReviewText = c(
    "The product is amazing!",
    "Terrible experience with customer service.",
    "Excellent quality and fast delivery.",
    "Not worth the money.",
    "Very happy with the purchase!",
    "Awful packaging, but product is okay.",
    "Highly recommend this to everyone.",
    "Worst experience ever.",
    "Great product, but shipping was delayed.",
    "Happy with the quality and support."
  )
)

# View the dataset
print(reviews)
Step 2: Filter Positive Reviews
# Filter reviews with the word "happy"
positive_reviews <- reviews[grepl("happy", reviews$ReviewText, ignore.case = TRUE), ]
print(positive_reviews)

Output:

  CustomerID                          ReviewText
5          5   Very happy with the purchase!
10        10 Happy with the quality and support.
Step 3: Filter Negative Reviews
# Filter reviews with negative words
negative_reviews <- reviews[grepl("terrible|worst|awful|not worth", reviews$ReviewText, ignore.case = TRUE), ]
print(negative_reviews)

Output:

  CustomerID                                ReviewText
2          2 Terrible experience with customer service.
4          4                        Not worth the money.
6          6       Awful packaging, but product is okay.
8          8                     Worst experience ever.
4. Exercise: Filter and Analyze Product Reviews

Task 1: Filter Specific Words

  • Find reviews containing the word "recommend".
  • Check if any reviews mention "delivery".

Task 2: Count Matches

Count how many reviews mention "quality".

Hint:
# Count reviews mentioning "quality"
count_quality <- sum(grepl("quality", reviews$ReviewText, ignore.case = TRUE))
cat("Number of reviews mentioning 'quality':", count_quality)
5. Advanced Example: Combine with Data Manipulation
# Add a new column for sentiment
reviews$Sentiment <- ifelse(
  grepl("happy|excellent|great|recommend", reviews$ReviewText, ignore.case = TRUE), "Positive", 
  ifelse(grepl("terrible|worst|awful|not worth", reviews$ReviewText, ignore.case = TRUE), "Negative", "Neutral")
)

# Summarize the sentiments
table(reviews$Sentiment)

Output:

Negative Neutral Positive 
       4       3        3

This shows the count of reviews in each sentiment category.

8. Comprehensive Dataset Exploration in R
1. What We’ll Cover

Here’s what you’ll learn today:

  1. Loading a Dataset.
  2. Exploring the Dataset – structure, summary, and dimensions.
  3. Handling Missing Values.
  4. Visualizing the Dataset – uncovering patterns and trends.
  5. Performing Summary Statistics.

We’ll use the famous mtcars dataset for this tutorial, which comes preloaded in R.

2. Step 1: Loading the Dataset

The mtcars dataset contains data about 32 car models, including miles per gallon (mpg), horsepower (hp), weight (wt), and more.

Code:
# Load the dataset
data(mtcars)

# Convert row names to a column for easier analysis
mtcars$CarModel <- rownames(mtcars)
rownames(mtcars) <- NULL
3. Step 2: Exploring the Dataset
View the first few rows:
# View the first few rows
head(mtcars)
Check the structure and dimensions:
# Structure of the dataset
str(mtcars)

# Dimensions of the dataset
dim(mtcars)

# Summary of the dataset
summary(mtcars)
Output Interpretation:
  • str(mtcars) shows variable names, types, and samples.
  • dim(mtcars) confirms there are 32 rows and 12 columns.
  • summary(mtcars) provides descriptive statistics for each column.
4. Step 3: Cleaning the Dataset
Code:
# Check for missing values
colSums(is.na(mtcars))

# Check for duplicates
duplicated_rows <- mtcars[duplicated(mtcars), ]
print(duplicated_rows)
Output:

If no missing values or duplicates are found, the dataset is clean. If found, we can handle them by:

  1. Removing rows with missing values:
    mtcars <- na.omit(mtcars)
  2. Removing duplicate rows:
    mtcars <- mtcars[!duplicated(mtcars), ]
5. Step 4: Summary Statistics
Find the mean, median, and standard deviation:
# Calculate mean, median, and standard deviation for mpg
mean_mpg <- mean(mtcars$mpg)
median_mpg <- median(mtcars$mpg)
sd_mpg <- sd(mtcars$mpg)

# Print the results
cat("Mean MPG:", mean_mpg, "\n")
cat("Median MPG:", median_mpg, "\n")
cat("Standard Deviation MPG:", sd_mpg, "\n")
Group-wise Summary:
# Group by number of cylinders and calculate the mean MPG
aggregate(mpg ~ cyl, data = mtcars, mean)
6. Step 5: Visualizing the Dataset
1. Distribution of Miles Per Gallon (mpg):
library(ggplot2)

# Histogram for MPG
ggplot(mtcars, aes(x = mpg)) +
  geom_histogram(binwidth = 2, fill = "skyblue", color = "black") +
  ggtitle("Distribution of Miles Per Gallon (MPG)") +
  xlab("Miles Per Gallon") +
  ylab("Frequency")
2. Relationship Between Weight and MPG:
# Scatterplot of Weight vs. MPG
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point(color = "red", size = 3) +
  ggtitle("Weight vs. MPG") +
  xlab("Weight (1000 lbs)") +
  ylab("Miles Per Gallon") +
  geom_smooth(method = "lm", color = "blue")
3. Bar Plot of Cylinder Count:
# Bar plot for number of cylinders
ggplot(mtcars, aes(x = factor(cyl))) +
  geom_bar(fill = "orange") +
  ggtitle("Number of Cars by Cylinder Count") +
  xlab("Cylinders") +
  ylab("Count")
4. Box Plot of Horsepower by Cylinders:
# Boxplot of horsepower by cylinder count
ggplot(mtcars, aes(x = factor(cyl), y = hp, fill = factor(cyl))) +
  geom_boxplot() +
  ggtitle("Horsepower by Cylinder Count") +
  xlab("Cylinders") +
  ylab("Horsepower")
7. Step 6: Advanced Insights
Correlation Matrix:
# Correlation matrix
correlation_matrix <- cor(mtcars[, sapply(mtcars, is.numeric)])
print(correlation_matrix)

# Visualize the correlation matrix
library(corrplot)
corrplot(correlation_matrix, method = "circle", type = "upper")
Linear Regression: Predict MPG Using Weight:
# Simple linear regression
model <- lm(mpg ~ wt, data = mtcars)
summary(model)

# Add regression line to scatterplot
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point(color = "blue") +
  geom_smooth(method = "lm", color = "red") +
  ggtitle("Linear Regression: MPG vs. Weight") +
  xlab("Weight (1000 lbs)") +
  ylab("Miles Per Gallon")
8. Step 7: Summarize Findings
  1. Distribution of MPG: Most cars have MPG between 15–25.
  2. Weight vs. MPG: Negative correlation—heavier cars have lower MPG.
  3. Cylinder Analysis: 4-cylinder cars generally have better fuel efficiency.
  4. Linear Regression: Weight significantly predicts MPG.
9. If-else statement in R Programming
2. What is an if-else Statement?
  • Definition of if-else: It is used for decision-making in R code.
  • Its purpose is to execute one block of code when a condition is TRUE and another block when the condition is FALSE.

Syntax Explanation with a Simple Example:

if (condition) {
  # Code to execute if condition is TRUE
} else {
  # Code to execute if condition is FALSE
}
3. Simple Example: Check if a Number is Positive or Negative
num <- -5
if (num > 0) {
  print("The number is positive")
} else {
  print("The number is negative")
}
  • The condition checks if num > 0.
  • The result is displayed using the print() function.
4. Using if-else with Multiple Conditions

This demonstrates how to use an if-else if structure to check if a number is positive, negative, or zero:

num <- 0
if (num > 0) {
  print("The number is positive")
} else if (num < 0) {
  print("The number is negative")
} else {
  print("The number is zero")
}
5. Practical Example: Grade Assignment

Assign grades based on marks:

marks <- 85
if (marks >= 90) {
  print("Grade: A+")
} else if (marks >= 80) {
  print("Grade: A")
} else if (marks >= 70) {
  print("Grade: B")
} else if (marks >= 60) {
  print("Grade: C")
} else {
  print("Grade: F")
}
  • Multiple conditions are evaluated sequentially.
  • Once a condition is true, the corresponding code block is executed and the rest are skipped.
6. Example with Logical Operators

Combine conditions using & (AND) and | (OR). For example, checking if a person is eligible to vote:

age <- 20
citizen <- TRUE
if (age >= 18 & citizen) {
  print("You are eligible to vote")
} else {
  print("You are not eligible to vote")
}
7. Vectorized ifelse()

Use ifelse() for efficient vectorized conditional operations. For example, categorizing numbers as "Even" or "Odd":

numbers <- c(1, 2, 3, 4, 5)
result <- ifelse(numbers %% 2 == 0, "Even", "Odd")
print(result)

Output:

[1] "Odd" "Even" "Odd" "Even" "Odd"
8. Common Mistakes and Debugging Tips
  • Forgetting curly braces {} when writing code on multiple lines.
  • Using the assignment operator = instead of the comparison operator == in conditions.

Example of debugging:

# Incorrect syntax:
x <- 10
if (x = 5) {  
  print("x is 5")
} else {
  print("x is not 5")
}

# Correct syntax:
if (x == 5) {
  print("x is 5")
} else {
  print("x is not 5")
}
9. Real-World Example: Discount Calculation
purchase_amount <- 120
if (purchase_amount > 100) {
  discount <- purchase_amount * 0.1
} else {
  discount <- 0
}
print(paste("Discount is:", discount))

This example shows a common use case of if-else for calculating discounts based on purchase amounts.

Advanced Examples
Example 1: Dynamic Tax Calculation Based on Income Brackets
# Input: Annual Income
income <- 95000

# Calculate Tax
if (income <= 25000) {
  tax <- 0
} else if (income <= 50000) {
  tax <- 0.1 * (income - 25000)
} else if (income <= 100000) {
  tax <- 2500 + 0.2 * (income - 50000)
} else {
  tax <- 12500 + 0.3 * (income - 100000)
}

# Print the Tax Amount
print(paste("The income tax for an income of", income, "is:", tax))

Explanation:

  • The if-else conditions apply different tax rates to specific income ranges.
  • Higher income brackets include cumulative tax from lower brackets.
  • For an income of 95000, the calculated tax is output.
Example 2: Employee Performance Bonus Calculation
# Input: Sales and Experience Level
sales <- 150000
experience <- 3  # in years

# Calculate Bonus
if (sales > 100000 & experience >= 5) {
  bonus <- 0.1 * sales  # 10% bonus
} else if (sales > 100000 & experience < 5) {
  bonus <- 0.08 * sales  # 8% bonus
} else if (sales <= 100000 & experience >= 5) {
  bonus <- 0.05 * sales  # 5% bonus
} else {
  bonus <- 0.03 * sales  # 3% bonus
}

# Print the Bonus Amount
print(paste("The performance bonus is:", bonus))

Explanation:

  • Conditions use the logical operator & to evaluate sales and experience simultaneously.
  • Different bonus percentages are applied based on the combination of sales and experience.
  • For the given example, the bonus is calculated and printed accordingly.
10. Switch statement in R Programming
2. What is a switch Statement?
  • Definition: A control structure used for selecting one of several options based on a value or condition.
  • Syntax:
switch(expression,
       case1 = result1,
       case2 = result2,
       case3 = result3,
       ...)
  • Benefits:
    • Cleaner and more readable code compared to multiple if-else statements.
    • Ideal for scenarios with a fixed set of choices.
3. Example 1: Day of the Week

Code:

day <- 3
result <- switch(day,
         "Monday",
         "Tuesday",
         "Wednesday",
         "Thursday",
         "Friday",
         "Saturday",
         "Sunday")
print(paste("The day is:", result))

Explanation:

  • The variable day determines which case is selected (1 for Monday, 2 for Tuesday, etc.).
  • Output for this example:
[1] "The day is: Wednesday"
4. Example 2: Basic Calculator

Code:

operation <- "multiply"
a <- 10
b <- 5

result <- switch(operation,
         add = a + b,
         subtract = a - b,
         multiply = a * b,
         divide = if (b != 0) a / b else "Division by zero is not allowed")
print(paste("The result is:", result))

Explanation:

  • The variable operation determines which calculation is performed.
  • An inline if statement is used to handle division by zero.
  • Output for operation = "multiply":
[1] "The result is: 50"
5. Using switch with Character Input

Example: Greet the user based on language preference.

language <- "French"

greeting <- switch(language,
         English = "Hello",
         Spanish = "Hola",
         French = "Bonjour",
         German = "Guten Tag",
         "Language not supported")
print(greeting)

Explanation: If language matches a case, the corresponding greeting is returned. Otherwise, the default case ("Language not supported") is used. Output for language = "French":

[1] "Bonjour"
6. Advanced Example: Menu-Based Program

Code:

menu_option <- 2

message <- switch(menu_option,
         "1" = "Start a new game",
         "2" = "Load saved game",
         "3" = "View high scores",
         "4" = "Exit")
print(message)

Explanation: The variable menu_option determines the action. Output for menu_option = 2:

[1] "Load saved game"
7. Practical Example: Month Days

Code:

month <- "February"

days <- switch(month,
         January = 31,
         February = 28,
         March = 31,
         April = 30,
         May = 31,
         June = 30,
         July = 31,
         August = 31,
         September = 30,
         October = 31,
         November = 30,
         December = 31,
         "Invalid month")
print(paste("Number of days in", month, ":", days))

Explanation:

  • Returns the number of days for the given month.
  • The default case ("Invalid month") is used if no match is found.
  • Output for month = "February":
[1] "Number of days in February : 28"
8. When to Use switch vs if-else
  • Use switch when: There is a fixed, predefined set of cases; each corresponds to a unique result.
  • Use if-else when: You need to evaluate complex conditions or ranges.
9. Common Mistakes and Debugging Tips
  • Case labels must match the input value type (numeric, character, etc.).
  • The default value (if no match) is given as the last argument without a label.
  • Debugging tip: Print intermediate variables to ensure they have the expected values.