Functional Programming in R with purrr | by Thomas Mock | Towards Data Science

When you first started in R you likely were writing simple code.
print("Hello world!")5 * 6x <- c(1, 2, 3, 4, 5)
This is great, you are learning about strings, math, and vectors in R!
Then you get started with some basic analyses. You want to see if you can find the mean of some numbers.
employee <- c('John Doe','Peter Gynn','Jolie Hope')salary <- c(21000, 23400, 26800)startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))# form dataframe and take mean of salary columnemploy_data <- data.frame(employee, salary, startdate)mean(employ_data$salary)# output[1] 23733.33
Eventually you hopefully get exposed to the tidyverse, and you find how this “ opinionated collection of R packages designed for data science” makes data analysis in R easier and more readable!
mtcars %>%   group_by(cyl) %>%   summarize(mean(mpg))# A tibble: 3 x 2    cyl `mean(mpg)`  <dbl>       <dbl>1  4.00        26.72  6.00        19.73  8.00        15.1
Everything is going great! You’ve likely replaced Excel at this point, and potentially SPSS or some other statistical software suite! But then you run into a problem where you need to use a function repeatedly.
You could use something like the following code to calculate one-way ANOVAs for some dependent variables and a set independent variable:
aov_mpg <- aov(mpg ~ factor(cyl), data = mtcars)summary(aov_mpg)aov_disp <- aov(disp ~ factor(cyll), data = mtcars)summary(aov_disp)aov_hp <- aov(hp ~ factor(cyl), data = mrcars)summry(aov_hpp)aov_wt <- aov(wt ~ factor(cyl), datas = mtcars)summary(aov_wt)
But you copy-pasted code 3x, and oops you made some minor misspelling mistakes which throws an error! (The above code leads to errors!)
Also, what if you realized that you wanted to actually run these ANOVAs for number of gears instead of number of cylinders? You would have to go back and change the factor(cyl) call to factor(gear) 4x! This is not very efficient, and you’re more likely to end up with mistakes as you have to type everything multiple times!
How about another example.
Let’s calculate the R-squared values for the linear relationship between Weight and Miles per Gallon, according to the number of Cylinders.
I have written code below that does this for 4 cylinder cars from the mtcars dataset. This is a worst case scenario, you know some dplyr code (dplyr::filter), but are not comfortable with the pipe. That’s fine, you accomplish your goal but a lot of coding! You would have to duplicate this code for 6 cylinder and 8 cylinder cars, for even more code...
# create df for 4 cylinder carscyl_4 <- filter(mtcars, cyl == 4)# create a linear model on 4 cyl carslm_4 <- lm(mpg ~ wt, data = cyl_4)# get the summlm_4_summary <- summary(lm_4)# get the r.squared valuelm_4_r_squared <- lm_4_summary["r.squared"]# check the valuelm_4cyl_r_squared# output$r.squared[1] 0.5086326
Alternatively, you could do the same thing with the pipe. A lot less typing, but to do this for all 3 subsets means we have to copy paste multiple times, so if you end up wanting to do this as a linear model of mpg ~ disp in addition to mpg ~ wt, you would have to duplicate the code 3 more times and change it 3 more times. This may not seem like a big deal, but eventually is a huge deal once you start to scale up the code (say 10+ times or 100+ times, etc).
lm_4cyl_rsquared <- mtcars %>%   filter(cyl == 4) %>%  lm(mpg ~ wt, data = .) %>%   summary() %>%   .$"r.squared"# output[1] 0.5086326
To solve this issue of minimizing repetition with further replication, we can dive straight into purrr! To read more about purrr Hadley Wickham recommends the iteration chapter from “R for Data Science” or alternatively you can look at the purrr documentation. Lastly, Jenny Bryan has a great purrr tutorial here. You can load purrr by itself, but it is also loaded as part of the tidyverse library.
purrr allows you to map functions to data. Appropriately the basic function in purrr is called map()! The map functions transform their input by applying a function to each element and returning a vector the same length as the input.
The base arguments for map() are:.x — A list or atomic vector (logical, integer, double/numeric, and character).f — A function, formula, or atomic vector
Basically map() takes a function (.f) and applies it to data (.x).
Going back to our example of grabbing the R-squared from a linear model, we use the following code with purrr.
mtcars %>%  split(.$cyl) %>%  map(~ lm(mpg ~ wt, data = .)) %>%  map(summary) %>%  map_dbl("r.squared")# output4         6         8 0.5086326 0.4645102 0.4229655
This generates an output from all 3 of our linear models according to number of cylinders in 5 lines of code! This is the beauty of purrr, efficient scaling of functions!
Let’s break down our linear model R-squared code.
We take the mtcars dataset, split it into data subsets according to the number of cylinders, apply a linear model of mpg by wt to each subset of data, apply a summary function and then pull out the r.squared value. However, while purrr is readable, we need to cover a few quirks of using it.
mtcars %>%  split(.$cyl) %>%  map(~ lm(mpg ~ wt, data = .)) %>%  map(summary) %>%  map_dbl("r.squared")# output4         6         8 0.5086326 0.4645102 0.4229655
For our code here you may have noticed we have a “.” placed twice within the code. This is a placeholder for the data, we can see this below. The “.” indicate the left-hand side data, or in this case mtcars. Our split call splits the mtcars dataframe into 3 dataframes, each stored within a list. This may seem odd, but it allows map to cycle through our 3 dataframes and replicate the lm() function on each of them individually.
# pipedmtcars %>%   split(.$cyl)# base Rsplit(mtcars, mtcars$cyl)# outputList of 3 $ 4:'data.frame': 11 obs. of  11 variables:  ..$ mpg : num [1:11] 22.8 24.4 22.8 32.4 30.4 33.9 21.5 27.3 26...  ..$ cyl : num [1:11] 4 4 4 4 4 4 4 4 4 4 ...  ..$ disp: num [1:11] 108 146.7 140.8 78.7 75.7 ...  ..$ hp  : num [1:11] 93 62 95 66 52 65 97 66 91 113 ...  ..$ drat: num [1:11] 3.85 3.69 3.92 4.08 4.93 4.22 3.7 4.08 ...  ..$ wt  : num [1:11] 2.32 3.19 3.15 2.2 1.61 ...  ..$ qsec: num [1:11] 18.6 20 22.9 19.5 18.5 ...  ..$ vs  : num [1:11] 1 1 1 1 1 1 1 1 0 1 ...  ..$ am  : num [1:11] 1 0 0 1 1 1 0 1 1 1 ...  ..$ gear: num [1:11] 4 4 4 4 4 4 3 4 5 5 ...  ..$ carb: num [1:11] 1 2 2 1 2 1 1 1 2 2 ... $ 6:'data.frame': 7 obs. of  11 variables:  ..$ mpg : num [1:7] 21 21 21.4 18.1 19.2 17.8 19.7  ..$ cyl : num [1:7] 6 6 6 6 6 6 6  ..$ disp: num [1:7] 160 160 258 225 168 ...  ..$ hp  : num [1:7] 110 110 110 105 123 123 175  ..$ drat: num [1:7] 3.9 3.9 3.08 2.76 3.92 3.92 3.62  ..$ wt  : num [1:7] 2.62 2.88 3.21 3.46 3.44 ...  ..$ qsec: num [1:7] 16.5 17 19.4 20.2 18.3 ...  ..$ vs  : num [1:7] 0 0 1 1 1 1 0  ..$ am  : num [1:7] 1 1 0 0 0 0 1  ..$ gear: num [1:7] 4 4 3 3 4 4 5  ..$ carb: num [1:7] 4 4 1 1 4 4 6 $ 8:'data.frame': 14 obs. of  11 variables:  ..$ mpg : num [1:14] 18.7 14.3 16.4 17.3 15.2 10.4 10.4 14.7...  ..$ cyl : num [1:14] 8 8 8 8 8 8 8 8 8 8 ...  ..$ disp: num [1:14] 360 360 276 276 276 ...  ..$ hp  : num [1:14] 175 245 180 180 180 205 215 230 150 150 ...  ..$ drat: num [1:14] 3.15 3.21 3.07 3.07 3.07 2.93 3 3.23 2.76...  ..$ wt  : num [1:14] 3.44 3.57 4.07 3.73 3.78 ...  ..$ qsec: num [1:14] 17 15.8 17.4 17.6 18 ...  ..$ vs  : num [1:14] 0 0 0 0 0 0 0 0 0 0 ...  ..$ am  : num [1:14] 0 0 0 0 0 0 0 0 0 0 ...  ..$ gear: num [1:14] 3 3 3 3 3 3 3 3 3 3 ...  ..$ carb: num [1:14] 2 4 3 3 3 4 4 4 2 2 ...
Similarily, the “.” in or first map call is a placeholder for data, but in this case it will cycle through our list of 3 dataframes generated by the previous pipe. You can see that we get a list of 3 lm() outputs, we need to map a summary call to each of these to get access to R-squared.
mtcars %>%  split(.$cyl) %>%  map(~ lm(mpg ~ wt, data = .))# output$`4`Call:lm(formula = mpg ~ wt, data = .)Coefficients:(Intercept)           wt       39.571       -5.647$`6`Call:lm(formula = mpg ~ wt, data = .)Coefficients:(Intercept)           wt        28.41        -2.78$`8`Call:lm(formula = mpg ~ wt, data = .)Coefficients:(Intercept)           wt       23.868       -2.192
We next map our summary function to each of the list items to get cleaner outputs with R-squared values. We now have the rest of our statistical output, including p values and R-squared.
mtcars %>%  split(.$cyl) %>%  map(~ lm(mpg ~ wt, data = .)) %>%  map(summary)# output$`4`Call:lm(formula = mpg ~ wt, data = .)Residuals:    Min      1Q  Median      3Q     Max -4.1513 -1.9795 -0.6272  1.9299  5.2523Coefficients:            Estimate Std. Error t value Pr(>|t|)    (Intercept)   39.571      4.347   9.104 7.77e-06 ***wt            -5.647      1.850  -3.052   0.0137 *  ---Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1Residual standard error: 3.332 on 9 degrees of freedomMultiple R-squared:  0.5086, Adjusted R-squared:  0.454 F-statistic: 9.316 on 1 and 9 DF,  p-value: 0.01374$`6`Call:lm(formula = mpg ~ wt, data = .)Residuals:     Mazda RX4  Mazda RX4 Wag Hornet 4 Drive        Valiant       Merc 280        -0.1250         0.5840         1.9292        -0.6897         0.3547      Merc 280C   Ferrari Dino        -1.0453        -1.0080Coefficients:            Estimate Std. Error t value Pr(>|t|)   (Intercept)   28.409      4.184   6.789  0.00105 **wt            -2.780      1.335  -2.083  0.09176 . ---Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1Residual standard error: 1.165 on 5 degrees of freedomMultiple R-squared:  0.4645, Adjusted R-squared:  0.3574 F-statistic: 4.337 on 1 and 5 DF,  p-value: 0.09176$`8`Call:lm(formula = mpg ~ wt, data = .)Residuals:    Min      1Q  Median      3Q     Max -2.1491 -1.4664 -0.8458  1.5711  3.7619Coefficients:            Estimate Std. Error t value Pr(>|t|)    (Intercept)  23.8680     3.0055   7.942 4.05e-06 ***wt           -2.1924     0.7392  -2.966   0.0118 *  ---Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1Residual standard error: 2.024 on 12 degrees of freedomMultiple R-squared:  0.423, Adjusted R-squared:  0.3749 F-statistic: 8.796 on 1 and 12 DF,  p-value: 0.01179
Our last map is a bit different. You can see we use map_dbl this time. This indicates we want our output to be a dbl or numeric outcome. We get nice named numbers!
mtcars %>%  split(.$cyl) %>%  map(~ lm(mpg ~ wt, data = .)) %>%  map(summary) %>%  map_dbl("r.squared")# output        4         6         8 0.5086326 0.4645102 0.4229655
If we had not indicated map_dbl, but instead used map we would get a list of the same outcome.
mtcars %>%  split(.$cyl) %>%   map(~ lm(mpg ~ wt, data = .)) %>%  map(summary) %>%  map("r.squared")# output$`4`[1] 0.5086326$`6`[1] 0.4645102$`8`[1] 0.4229655
You could also use map_dfr which binds the outputs into rows of a dataframe.
mtcars %>%  split(.$cyl) %>%   map(~ lm(mpg ~ wt, data = .)) %>%  map(summary) %>%  map_dfr("r.squared")# output# A tibble: 1 x 3    `4`   `6`   `8`  <dbl> <dbl> <dbl>1 0.509 0.465 0.423
There are limitless applications of purrr and other functions within purrr that greatly empower your functional programming in R. I hope that this guide motivates you to add purrr to your toolbox and explore this useful tidyverse package!
As a brief teaser to some more applications of purrr, I’ll leave you with this example. I mentioned calculating ANOVAs across multiple variables at the beginning. Break down this example on your own and see what you think! (You can copy paste this code into R, but need to load the tidyverse and broom packages first).
mtcars %>%  mutate(cyl = factor(cyl),         am = factor(am)) %>%  select(mpg, disp, hp) %>%  map(~ aov(.x ~ cyl * am, data = mtcars)) %>%  map_dfr(~ broom::tidy(.), .id = 'source') %>%  mutate(p.value = round(p.value, 5))
In closing, I’d like to thank several #r4ds slack community members for their help in my personal understanding of purrr: Frank Farach, Michael Kuehn, and Kent Johnson.
If you are interested in joining this community led by Jesse Maegan check out her post here and May the Force be with you!