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
andplotly
- Machine Learning
- Data Cleaning
- Bioinformatics
- Time Series Analysis
Steps to Install R:
- Visit the CRAN website.
- Select your operating system (Windows, macOS, or Linux).
- Download and install R.
Steps to Install R Studio:
- Go to the R Studio website.
- Download and install R Studio.
Tip: Install R before R Studio.
- 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
.
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
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")
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.
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.
- 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
- Use meaningful names like
total_sales
. - Avoid names like
mean
orsum
. - Stick to naming styles:
snake_case
orcamelCase
.
my_age
and create a vector:
my_age <- 50
my_numbers <- c(5, 10, 15, 20)
price <- 100
price <- price + 50 # Now, price is 150
Q1: What is the value of z
?
x <- 10
y <- x * 2
z <- y + 5
Answer: 25
Q2: Is total.sales
valid?
Answer: Yes
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)
A vector is the simplest data structure in R. It contains elements of the same type: numeric, character, or logical.
Use the c()
function to combine values into a vector.
# 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)
print(numeric_vector[1]) # Access the first element
sum(numeric_vector) # Calculate sum
mean(numeric_vector) # Calculate mean
- 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.
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)
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)
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)
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
- 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)
andc(1, 3, 5, 7)
. - Multiply elements of
c(3, 6, 9)
. - Mean of
c(20, 25, 30, 35, 40)
. - Compare
c(10, 15, 20)
withc(12, 15, 18)
.
- Create monthly expenses:
c(2000, 2500, 1800, 2200, 2700, 3000)
. - Calculate total expenses over 6 months.
- Identify months where expenses > 2500.
- Reduce expenses by 10% and display updated values.
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!
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.).
Data frames are the primary way to store and manipulate structured data in R. They are widely used for:
- Data analysis and exploration.
- Data cleaning and transformation.
- Integration with various machine learning and visualization tools.
# 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
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")])
# 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)
# 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)
# Sort by Age
sorted_by_age <- employees[order(employees$Age), ]
# Sort by Salary (descending)
sorted_by_salary <- employees[order(-employees$Salary), ]
# Read from a CSV file
employees <- read.csv("employees.csv")
# Write to a CSV file
write.csv(employees, "updated_employees.csv", row.names = FALSE)
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
andcyl == 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.
- 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.
# 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)
The if
statement is used to execute code when a condition is true.
if (condition) {
# Code to execute if condition is TRUE
}
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:
Use an if
statement to verify if a number is even and divisible by another number.
x <- 20
if (x %% 2 == 0 && x %% 5 == 0) {
print(paste(x, "is even and divisible by 5"))
}
Let’s break down the provided R function into its individual components and explain what each part means:
x <- 20
if (x %% 2 == 0 && x %% 5 == 0) {
print(paste(x, "is even and divisible by 5"))
}
x <- 20
x
: A variable that stores the value20
.<-
: The assignment operator.
if (x %% 2 == 0 && x %% 5 == 0)
x %% 2 == 0
: Checks ifx
is divisible by 2.&&
: Logical AND operator.x %% 5 == 0
: Checks ifx
is divisible by 5.
{
print(paste(x, "is even and divisible by 5"))
}
This block executes only if the if
condition is true.
Use if
to determine if a specific value exists in a vector.
vec <- c(10, 20, 30, 40)
val <- 25
if (val %in% vec) {
print(paste(val, "exists in the vector"))
}
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.
x <- 45
if (x > 10) {
if (x %% 5 == 0) {
print(paste(x, "is greater than 10 and divisible by 5"))
}
}
a <- 25
b <- 30
if (a < b) {
print(paste(a, "is less than", b))
}
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")
}
text <- "R programming is fun"
if (grepl("programming", text)) {
print("The word 'programming' is found in the text")
}
text <- "R programming is fun"
if (grepl("programming", text)) {
print("The word 'programming' is found in the text")
}
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.
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 isFALSE
). - fixed: If
TRUE
, thepattern
is treated as a literal string, not a regular expression.
grepl()
# 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.
# Case-insensitive search for "APPLE"
result <- grepl("APPLE", text_vector, ignore.case = TRUE)
print(result)
Output:
[1] TRUE FALSE FALSE FALSE
# 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".
# 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)
# 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.
# 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.
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".
# Count reviews mentioning "quality"
count_quality <- sum(grepl("quality", reviews$ReviewText, ignore.case = TRUE))
cat("Number of reviews mentioning 'quality':", count_quality)
# 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.
Here’s what you’ll learn today:
- Loading a Dataset.
- Exploring the Dataset – structure, summary, and dimensions.
- Handling Missing Values.
- Visualizing the Dataset – uncovering patterns and trends.
- Performing Summary Statistics.
We’ll use the famous mtcars
dataset for this tutorial, which comes preloaded in R.
The mtcars
dataset contains data about 32 car models, including miles per gallon (mpg
), horsepower (hp
), weight (wt
), and more.
# Load the dataset
data(mtcars)
# Convert row names to a column for easier analysis
mtcars$CarModel <- rownames(mtcars)
rownames(mtcars) <- NULL
# View the first few rows
head(mtcars)
# Structure of the dataset
str(mtcars)
# Dimensions of the dataset
dim(mtcars)
# Summary of the dataset
summary(mtcars)
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.
# Check for missing values
colSums(is.na(mtcars))
# Check for duplicates
duplicated_rows <- mtcars[duplicated(mtcars), ]
print(duplicated_rows)
If no missing values or duplicates are found, the dataset is clean. If found, we can handle them by:
- Removing rows with missing values:
mtcars <- na.omit(mtcars)
- Removing duplicate rows:
mtcars <- mtcars[!duplicated(mtcars), ]
# 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 by number of cylinders and calculate the mean MPG
aggregate(mpg ~ cyl, data = mtcars, mean)
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")
# 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")
# 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")
# 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")
# 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")
# 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")
- Distribution of MPG: Most cars have MPG between 15–25.
- Weight vs. MPG: Negative correlation—heavier cars have lower MPG.
- Cylinder Analysis: 4-cylinder cars generally have better fuel efficiency.
- Linear Regression: Weight significantly predicts MPG.
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 isFALSE
.
Syntax Explanation with a Simple Example:
if (condition) {
# Code to execute if condition is TRUE
} else {
# Code to execute if condition is FALSE
}
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.
if-else
with Multiple ConditionsThis 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")
}
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.
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")
}
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"
- 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")
}
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.
# 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.
# 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.
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.
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"
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"
switch
with Character InputExample: 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"
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"
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"
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.
- 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.