How to use mapply in R
Introduction
The mapply() function in R applies a function to multiple vectors or lists simultaneously, element by element. It’s particularly useful when you need to perform operations on corresponding elements from different data structures, making it more efficient than writing loops.
Getting Started
library(tidyverse)
library(palmerpenguins)Example 1: Basic Usage
The Problem
We want to calculate the sum of corresponding elements from three different numeric vectors. Instead of writing a loop, we can use mapply() to apply the sum function across all vectors simultaneously.
Step 1: Create sample vectors
Let’s start with three simple numeric vectors of equal length.
vector1 <- c(10, 20, 30, 40)
vector2 <- c(5, 15, 25, 35)
vector3 <- c(2, 4, 6, 8)We now have three vectors with four elements each that we want to sum element-wise.
Step 2: Apply mapply with sum function
We’ll use mapply() to sum corresponding elements across all three vectors.
result <- mapply(sum, vector1, vector2, vector3)
print(result)The result shows 17 39 61 83, which represents the sum of corresponding elements from each position.
Step 3: Compare with manual calculation
Let’s verify our result by calculating one position manually.
# Manual calculation for first position
manual_sum <- vector1[1] + vector2[1] + vector3[1]
print(paste("Manual:", manual_sum, "vs mapply:", result[1]))This confirms that mapply() correctly summed the first elements: 10 + 5 + 2 = 17.
Example 2: Practical Application
The Problem
We want to create customized labels for penguins data by combining species names with their body mass ranges. We need to apply a paste function to multiple columns simultaneously to generate meaningful descriptions for data visualization.
Step 1: Prepare the penguin data
First, let’s extract the relevant columns and remove missing values.
penguin_data <- penguins |>
filter(!is.na(body_mass_g)) |>
select(species, body_mass_g) |>
slice_head(n = 6)We now have clean data with species names and body mass measurements for six penguins.
Step 2: Create weight categories
We’ll categorize penguins as “Light” or “Heavy” based on their body mass.
categories <- ifelse(penguin_data$body_mass_g > 4000, "Heavy", "Light")
print(categories)This creates a character vector with weight categories that correspond to each penguin.
Step 3: Use mapply to create custom labels
Now we’ll combine species names with weight categories using mapply().
custom_labels <- mapply(
paste,
penguin_data$species,
categories,
sep = " - "
)The mapply() function applies paste() to corresponding elements from both vectors with a separator.
Step 4: Display the results
Let’s see our custom labels alongside the original data.
result_df <- data.frame(
species = penguin_data$species,
mass = penguin_data$body_mass_g,
label = custom_labels
)
print(result_df)Each row now shows the original species, body mass, and our custom label combining both pieces of information.
Summary
mapply()applies functions to corresponding elements across multiple vectors or lists simultaneously- It eliminates the need for explicit loops when working with parallel data structures
- The function is particularly useful for element-wise operations on multiple datasets
mapply()works with any function that can accept multiple arguments, including custom functionsAlways ensure your input vectors have the same length to avoid unexpected recycling behavior