Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Yesterday Hadley’s functional programming package purrr was published to CRAN. It is designed to bring convenient functional programming paradigma and add another data manipulation framework to R.
“Where dplyr focusses on data frames, purrr focusses on vectors” – Hadley Wickham in a blogpost
The core of the package consists of map functions, which operate similar to base apply functions. So I tried to rebuild the first example used to present the map function in the blog and Github repo with apply. The example looks like this:
Let’s do it with apply now:
As you can see, map works with 3 different inputs – function names (exactly like apply), anonymous functions as formulae or character to select elements. Apply on the other hand only accepts functions, but with a little piping voodoo we can also shortcut the anonymous functions like this:
Which is almost as appealing as the map alternative. Checking the speed of both approaches also reveals no significant time differences:
Now don’t get me wrong, I don’t want to say, that purrr is worthless or reduntant. I just picked the most basic function of the package and explained what it does by rewriting with apply functions. The map function and their derivatives are convenient alternatives to apply in my eyes without computational overhead. Furthermore the package offers very interesting functions like zip_n
and lift
. If you love apply and work with lists, you definitely should check out purrr
.
Rebuilding Map Example With Apply Functions was originally published by Kirill Pomogajko at Opiate for the masses on September 30, 2015.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.