Functional Programming in R Using “Purrr” Package

If you are here you are probably familiar with R language(Ever wondered why it called ‘R’, It was initially named as S obviously stands for Statistical computing but the authors; Ross Ihaka and Robert Gentleman named it is as R after their first name’s first letter.) and now you(If you are a beginner) are wondering why have I never heard about Purrr package.

“R is a Functional Programming Language which means R provides a set of tools to create and manipulate functions ”

Hadley Wickham and Lionel Henry are the authors of Purrr and it is a part of Tidyverse. Functions are also objects in R they are treated in the same way as Vectors:

create, assign them to objects and later use these objects in other functions and another important feature is passing functions as arguments to other functions: the core part of apply family functions as well as the Purrr package.

What’s the point of using Purrr package since we already have the Apply family functions. BTW If you are not familiar with the apply family then here’s the link.

Passing functions as arguments make ‘R’ users hassle-free. In other words less code and less verbose. Now, Let’s see the first function in the Purrr package called Map.

Map function’s syntax map (.x, .f, …) is pretty much same as the Apply family functions except mapply. In mapply, the function argument comes first then followed by data arguments.

The first argument .x is always a vector followed by .f is always a function which is applied to every element of .x. These map functions work on the same logic as Apply family functions.

Application of Purrr Package in R

Let’s create a Dataframe:

 

 

The significance of … in a map function

The … (dot dot dot)  argument in map functions is used to pass the additional arguments to the function .f and the dot (.) before x and f denote that these argument names are highly unlikely to be the argument names which we pass through …, To avoid the confusion: the first two arguments belong to the map function and the rest of them belong to the function that we are mapping.

Why Map over sapply:

Type-Inconsistent or Unstable functions: The type of return object depends on the input. sapply is a Type-Inconsistent function.

All the Purrr functions are Type-Consistent or stable, which means they will always return the type you are expecting regardless of the input.

There are a plethora of map functions depending upon the return type of the object.

  • map()returns a list
  • map_lgl()returns a logical vector
  • map_int()returns an integer vector
  • map_dbl()returns a double vector
  • map_chr()returns a character vector.

 

Ways of specifying .f in map functions and shortcuts for subsetting

 

This is important, The real application of these shortcuts are much more useful in the following scenario:
You built several linear models and you want to compare the “r square” or Accuracy of the same models. So to do that, Save those models in a list then by using the shortcuts provided by the map functions.

 

What if we want a function to iterate over 2 arguments, map2 and walk2 to the rescue!

map2(.x.y.f….), The syntax is similar to map function but has an additional argument “.y”, which is used to iterate over another object.

The function will take the .x‘s first element as the first argument and the .y‘s first element as the second argument and so on till the last elements of .x and .y.

What if we want a function to iterate over 3 or more objects, so for that Purrr provides a function called pmap or map_n rather than map3,map4 and so on.

These are some of the functions of Purrr package, See R’s Documentation on Purrr for some more functions.

You might also like More from author