Filtering a list with the Filter higher-order function
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Last week markbulling over at Drunks & Lampposts posted a method of using sapply
to filter a list by a predicate. Today the @RLangTip tip of the day was to use sapply
similarly. This made makes me wonder if R‘s very useful higher-order functions aren’t as well known as they should be. In this case, the Filter
higher-order function would be the tool to use. Filter
works more or less like the *apply
family of functions, but it performs the subsetting (the filtering) of a list based on a predicate in a single step.
As an example, let’s say we have a list of 1000 vectors, each of length 2 with \(x_1,\,x_2 \in [0,\,1]\), and we want to select only those vectors where the elements of the list sum to a value greater than 1. With Filter
, this is all we have to do:
mylist <- lapply(1:1000, function(i) c(runif(1), runif(1))) method.1 <- Filter(function(x) sum(x) > 1, mylist)
Which is at least a bit more transparent than the sapply
alternative:
method.2 <- mylist[sapply(mylist, function(x) sum(x) > 1)]
In some very quick tests, I found no performance difference between the two approaches.
There are other useful higher-order functions. If you are interested, check out ?Filter
.
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.