Functions are like recipes - they take ingredients (inputs), follow a set of instructions, and produce a result (output). In R, functions help us: - Avoid repeating code - Make our code more organized - Make our code reusable
A function in R has this basic structure:
function_name <- function(parameter1, parameter2) {
# Function body - what the function does
result <- # some operation
return(result)
}
Let’s start with some very simple functions:
# 1. A function to calculate the square of a number
square_number <- function(x) {
result <- x * x
return(result)
}
# Try it out
square_number(5) # Returns 25
## [1] 25
square_number(3) # Returns 9
## [1] 9
# 2. A function to greet someone
greet_person <- function(name) {
greeting <- paste("Hello,", name, "!")
return(greeting)
}
# Try it out
greet_person("Alice")
## [1] "Hello, Alice !"
greet_person("Bob")
## [1] "Hello, Bob !"
# 1. Calculate rectangle area
calculate_rectangle_area <- function(length, width) {
area <- length * width
return(area)
}
# Try it out
calculate_rectangle_area(5, 3) # Returns 15
## [1] 15
# 2. Create a personalized message
create_message <- function(name, age, hobby) {
message <- paste(name, "is", age, "years old and loves", hobby)
return(message)
}
# Try it out
create_message("Sarah", 25, "painting")
## [1] "Sarah is 25 years old and loves painting"
Let’s create some functions that work with data:
# Create sample data
student_scores <- data.frame(
name = c("Alice", "Bob", "Charlie", "Diana"),
math = c(85, 92, 78, 95),
science = c(92, 88, 85, 90),
history = c(88, 85, 82, 87)
)
# 1. Function to calculate average score for a student
calculate_average <- function(math, science, history) {
avg <- (math + science + history) / 3
return(round(avg, 1))
}
# Apply to our data
student_scores$average <- with(student_scores,
calculate_average(math, science, history))
# Show results
student_scores
## name math science history average
## 1 Alice 85 92 88 88.3
## 2 Bob 92 88 85 88.3
## 3 Charlie 78 85 82 81.7
## 4 Diana 95 90 87 90.7
# 2. Function to determine grade based on score
determine_grade <- function(score) {
if (score >= 90) {
return("A")
} else if (score >= 80) {
return("B")
} else if (score >= 70) {
return("C")
} else {
return("D")
}
}
# Apply to our average scores
student_scores$grade <- sapply(student_scores$average, determine_grade)
# Show results
student_scores
## name math science history average grade
## 1 Alice 85 92 88 88.3 B
## 2 Bob 92 88 85 88.3 B
## 3 Charlie 78 85 82 81.7 B
## 4 Diana 95 90 87 90.7 A
Sometimes we want functions to have default values for parameters:
# Function to calculate tip
calculate_tip <- function(bill_amount, tip_percentage = 15) {
tip <- bill_amount * (tip_percentage / 100)
return(round(tip, 2))
}
# Try it out
calculate_tip(50) # Uses default 15% tip
## [1] 7.5
calculate_tip(50, 20) # Specifies 20% tip
## [1] 10
Functions can return multiple values using a list:
# Function to analyze a numeric vector
analyze_numbers <- function(numbers) {
result <- list(
mean = mean(numbers),
median = median(numbers),
std_dev = sd(numbers),
range = max(numbers) - min(numbers)
)
return(result)
}
# Try it out with some numbers
test_scores <- c(85, 92, 78, 95, 88, 90)
analysis <- analyze_numbers(test_scores)
# Access individual results
analysis$mean
## [1] 88
analysis$median
## [1] 89
analysis$std_dev
## [1] 5.966574
analysis$range
## [1] 17
Try creating these functions on your own:
Here are the solutions:
# 1. Temperature conversion
fahrenheit_to_celsius <- function(fahrenheit) {
celsius <- (fahrenheit - 32) * (5/9)
return(round(celsius, 1))
}
# Test it
fahrenheit_to_celsius(72)
## [1] 22.2
# 2. Sum and product function
calculate_sum_product <- function(numbers) {
list(
sum = sum(numbers),
product = prod(numbers)
)
}
# Test it
calculate_sum_product(c(2, 3, 4))
## $sum
## [1] 9
##
## $product
## [1] 24
# 3. Student report function
create_student_report <- function(name, scores) {
avg_score <- mean(scores)
max_score <- max(scores)
min_score <- min(scores)
report <- paste(
"Student Report for", name, "\n",
"Average Score:", round(avg_score, 1), "\n",
"Highest Score:", max_score, "\n",
"Lowest Score:", min_score
)
return(report)
}
# Test it
test_scores <- c(85, 92, 78, 95)
create_student_report("Alice", test_scores)
## [1] "Student Report for Alice \n Average Score: 87.5 \n Highest Score: 95 \n Lowest Score: 78"
Try modifying these examples and creating your own functions to get more practice!