What is map() Function in R

In R, the map() function is part of the purrr package, allows you to apply a function to each element of a vector or list, and returns the results as a new list.

Syntax

map(rv, func, ...)

Parameters

The rv is an atomic vector or list.

The func is a function, formula, or atomic vector.

  1. If it is a function, it is used as it is.
  2. If it is a formula, e.g., ~ .x + 1, it is converted to a function. There are three ways to refer to the arguments:

    1. If it is a single argument function, use it.
    2. If there are two arguments function, use .x and .y.
    3. If there are more arguments, use ..1, ..2, ..3, etc.

Return Value

The map() function returns a vector of the same length as the input.

Example 1: How to use the map() function in R

Pass a function in the argument, and the map() function applies this function to all the vector elements.

# Pro.R

library(purrr)

rv <- 1:3
rv %>%
 map(function(x) x + 1)

Output

[[1]]
[1] 2

[[2]]
[1] 3

[[3]]
[1] 4

First, we import the purrr package using the library() statement.

In the next step, we define a vector and increment each vector value by 1 using the map() function. You can see that the map() function applies +1 to every vector element, and it does not modify the length of the vector.

Example 2: Applying the map() function to list

To apply the same operations on all the list elements, use the map() function. It returns the transformed list of the same size as an input list.

library(purrr)

list_data <- list(19, 21, 46)
list_data %>%
 map(function(x) x ^ 2)

Output

[[1]]
[1] 361

[[2]]
[1] 441

[[3]]
[1] 2116

We defined a list and used the map() function to transform every element into the input list of elements squared.

Example 3: Calculate the normal distributions from an atomic vector

You can calculate the normal distributions from the vector using the map() function.

library(purrr)

1:10 %>%
 map(function(x) rnorm(10, x))

Output

[[1]]
 [1] 0.7827287 0.8524386 1.0590045 -1.2431661 0.4175271 1.4974232
 [7] 0.2870406 0.5992063 1.4848386 1.6218882

[[2]]
 [1] 1.00669429 1.53255379 2.04919607 1.73777599 1.40305088 1.78990628
 [7] 0.72417310 0.68788464 0.73435835 -0.08438977

[[3]]
 [1] 4.517348 4.074736 3.678984 1.685608 3.078273 4.279211 2.774305 2.994225
 [9] 4.006520 4.695956

[[4]]
 [1] 2.365310 4.790439 4.514782 3.892882 2.655238 4.880776 3.393000 2.039758
 [9] 4.849628 2.765497

[[5]]
 [1] 5.227805 7.523240 5.431224 3.455294 5.457139 3.887880 3.846306 5.197063
 [9] 5.574604 4.019978

[[6]]
 [1] 7.095229 6.684091 4.094152 7.302912 8.209158 6.993704 5.047641 7.105118
 [9] 7.331152 5.688645

[[7]]
 [1] 5.905737 5.314832 6.886748 8.110370 6.005540 5.490614 8.844083 7.163475
 [9] 5.294017 8.072895

[[8]]
 [1] 8.455733 8.217845 8.385706 8.577377 7.520048 7.347701 6.831893 6.173750
 [9] 8.633676 8.496391

[[9]]
 [1] 6.979906 8.384546 9.342730 9.802514 9.958217 8.582408 9.607740 8.294869
 [9] 9.111597 9.483675

[[10]]
 [1] 9.229792 9.164576 9.420438 11.296276 9.494806 10.186833 10.290970
 [8] 9.919838 8.789936 10.279902

Conclusion

The map() function in R applies a function to each element of a vector or list and returns a list as a result. You must install and load the purrr package before using the map() function.

The map() methods(map and other variants) transform their input by applying a function to each item of a vector or list and returning the object with the same length as the input.

Leave a Comment