Programming with lapply
[This article was first published on tshafer.com, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
TIL that lapply
accepts both functions and function names (as
character vectors). From right there in the documentation
(emph. mine):
FUN
is found by a call tomatch.fun
and typically is specified as a function or a symbol (e.g., a backquoted name) or a character string specifying a function to be searched for from the environment of the call tolapply
.
So, lapply
can use match.fun
to find and apply our functions
directly; no need to hack around when we need custom function
application. That’s way simpler in cases like we’ve just had
recently at work, where we need to apply one of a variety of
functions depending on the program’s state.
This post is kindly republished by R-bloggers.
To leave a comment for the author, please follow the link and comment on their blog: tshafer.com.
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.