higher level functions in R - is there an official compose operator or curry function?
The standard place for functional programming in R is now the functional
library.
From the library:
functional: Curry, Compose, and other higher-order functions
Example:
library(functional)
newfunc <- Curry(oldfunc,x=5)
CRAN:
https://cran.r-project.org/web/packages/functional/index.html
PS: This library substitutes the ROxigen
library.
Currying functions in R
It is possible to curry in R, and there is a definition in the ROxygen package. See the discussion here
How to pass some (but not all) further arguments with ...
One way is to just make value a named parameter but ignore the input - that way it will never be a part of the dots to begin with.
f <- function(pattern, x, value = "hahaThisGetsIgnored", ...){
grep(pattern, x, value = TRUE, ...)
}
You can also use the idea in the answer @MatthewLundberg gave but by doing the argument manipulation without Curry so you don't need the functional package
f <- function(pattern, x, ...){
dots <- list(...)
# Remove value from the dots list if it is there
dots$value <- NULL
args <- c(list(pattern = pattern, x = x, value = TRUE), dots)
do.call(grep, args)
}
How to concatenate/compose functions in R?
For this you can use the Compose
function in the functional
package, which is available on CRAN.:
library(functional)
sapply(x, Compose(unique,sum))
What is the practical use of the identity function in R?
Don't know about R, but in a functional language one often passes functions as arguments to other functions. In such cases, the constant function (which returns the same value for any argument) and the identity function play a similar role as 0 and 1 in multiplication, so to speak.
How to pass arguments to a function inside *apply family functions
You can pass arguments in all the functions of the apply
family through the ellipsis (...
) argument, cf. the help page on sapply. Now, apply.daily
is just an extension to xts
objects, see ?apply.daily
.
apply.daily(tt2, mean, na.rm=TRUE)
#apply.daily( x, FUN, ...)
Related Topics
Rolling Join Grouped by a Second Variable in Data.Table
How to Manage a Table/Matrix to Obtain Information Using Conditions
Rstudio Calls Source() When Saving Script
Error in Bind_Rows_(X, .Id):Argument 1 Must Have Names
How to Rbind Only the Common Columns of Two Data Sets
How to Rearrange an Order of Matches Between Two Data Frames
Find the Source File Containing R Function Definition
Print a List of Dynamically-Sized Plots in Knitr
R + Ggplot2: How to Hide Missing Dates from X-Axis
Use Sprintf() to Add Trailing Zeros
Ggplot2: Creating Themed Title, Subtitle with Cowplot
Using Lm in List Column to Predict New Values Using Purrr
How to Merge Two Nodes into a Single Node Using Igraph
Include Text Control Characters in Plotmath Expressions
How to Rbind All the Data.Frames in Your Working Environment
Robust and Clustered Standard Error in R for Probit and Logit Regression