The Map
function in R is a type of apply function, designed to make operations over vectors or lists. The function takes a function f
and any number of vectors or lists, applying f
to the corresponding elements of the inputs. In R Programming Language the Map function is a useful function used for element-wise operations across vectors or lists.
Syntax:
Map(f, ...)
Parameters:
- f is the function to apply
- ... represents the vectors or lists to which the function is applied.
1. Calculating Mean using Map Function
We will first install and load the purrr package to utilize the map() function, which allows us to apply a function to each element of a list. Next, we will create a list called numbers_list containing three named vectors (a, b and c).
We will then display the original list to check its contents. After that, we will use the map() function to apply the mean() function to each vector in the list, calculating the mean for each one. Finally, we will print the mean_list to display the calculated means for each of the vectors.
R
install.packages("purrr")
library(purrr)
numbers_list <- list(
a = c(1, 2, 3),
b = c(4, 5, 6),
c = c(7, 8, 9)
)
cat("Original list:\n")
numbers_list
mean_list <- map(numbers_list, mean)
cat("\nMean of each vector in the list:\n")
mean_list
Output:
Mean using Map Function2. Addition of Corresponding Elements using the Map Function
We will create two vectors, x and y, containing the numbers 1 to 5 and 6 to 10, respectively. Then, we will use the Map() function to apply the addition (+) operation element-wise to corresponding elements of x and y. The Map() function will return a list containing the sum of each corresponding pair of elements from x and y. Finally, we will display the result.
R
x <- 1:5
y <- 6:10
result <- Map(`+`, x, y)
result
Output:
Addition of Corresponding Elements using the Map FunctionWe can also perform this on string values. We will create two vectors: names containing a list of names and suffixes containing corresponding titles for each name. Using the Map() function, we will combine each name with its respective title by applying the paste() function to the suffixes and names vectors. The sep=" " argument ensures there is a space between the title and the name.
R
names <- c("Anna", "Bob", "Charlie")
suffixes <- c("Ms.", "Mr.", "Dr.")
full_names <- Map(paste, suffixes, names, sep=" ")
full_names
Output:
map() Function in R3. Map Function using Multiple Lists
We can perform map function on multiple lists. We will define two lists, list1 and list2, each containing named vectors. Using the Map() function, we will concatenate the corresponding elements from both lists. The c function is used to combine each pair of vectors from the lists element-wise
R
list1 <- list(a = 1:3, b = 4:6)
list2 <- list(c = 7:9, d = 10:12)
result <- Map(c, list1, list2)
result
Output:
Map Function using Multiple Lists4. Applying Map Function on Built-in Functions
We will define a list of strings, strings and use the Map() function along with the nchar() function to calculate the length of each string in the list.
R
strings <- list("apple", "banana", "orange")
lengths <- Map(nchar, strings)
lengths
Output:
Applying Map Function on Built-in FunctionsThese examples showcase how the Map
function can be used in R to apply functions across corresponding elements of vectors or lists.