How to efficiently partially apply a function in R?

k13

Suppose I have a function in R that takes multiple arguments, and I'd like to reduce it to a function of fewer arguments by setting some of the arguments to pre-specified values. I'm trying to figure out what is the best way to do this is in R.

For example, suppose I have a function

f <- function(a,b,c,d){a+b+c+d}

I'd like to create or find a function partial that would do the following

partial <- function(f, ...){
#fill in code here
}
new_f <- partial(f, a=1, c= 2)

new_f would be a function of b and d and would return 1+b+2+d

In python I would do

from functools import partial

def f(a,b,c,d):
    return a+b+c+d

new_f = partial(f, a=1, c= 2)

I'm actually doing this repeatedly and so I need for this to be as efficient as possible. Can anyone point me to the most efficient way to do this? Right now the best I can do is

partial <- function(f, ...){
    z <- list(...)
    formals(f) [names(z)] <- z
    f
}

Can anyone let me know of a faster way or the best way to do this? This is simply too slow.

Rich Scriven

There are functions in the pryr package that can handle this, namely partial()

f <- function(a, b, c, d) a + b + c + d 
pryr::partial(f, a = 1, c = 2)
# function (...) 
# f(a = 1, c = 2, ...)

So you can use it like this -

new_fun <- pryr::partial(f, a = 1, c = 2)
new_fun(b = 2, d = 5)
# [1] 10
## or if you are daring ...
new_fun(2, 5)
# [1] 10

You could also simply change f()'s formal arguments with

f <- function(a, b, c, d) a + b + c + d 
formals(f)[c("a", "c")] <- list(1, 2)
f
# function (a = 1, b, c = 2, d) 
# a + b + c + d
f(b = 2, d = 5)
# [1] 10

But with the latter, you must name the b and d arguments in f() to avoid an error when you want to leave a and c as their default values.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I partially apply an infix function like Basics.+?

From Dev

How do you partially apply a function with multiple arguments?

From Dev

Partially apply function n times

From Dev

How to efficiently apply a function to each DataFrame of a Pandas Panel

From Dev

How to apply my own function with r::apply()

From Dev

How to partially (re)apply bindings to descendants with knockout?

From Dev

Efficiently apply function to neuron outputs then sum, not apply function to sum

From Dev

How to write this function efficiently?

From Dev

How to apply a function that creates columns to dataframe in R

From Dev

How to apply a function to factored subgroups in R?

From Dev

How to apply function by groups in array in R?

From Dev

How to apply a function to several variables in R?

From Dev

How to efficiently apply a two variable function to data.frame( or matrix) elements - to fill an output matrix?

From Dev

Clojure - more idiomatic to return a closure, or partially apply the function?

From Dev

How to efficiently apply a regex substitution on a Moose attribute?

From Dev

J: How to efficiently apply a verb to prefixes of suffixes?

From Dev

How to nest quantile() function within apply() function in R or RStudio

From Dev

How to pass another function as a variable to apply function in R?

From Dev

R - function, matrix, apply

From Dev

R: Custom function in apply()

From Dev

How to efficiently pass function through?

From Dev

How to use the strip() function efficiently

From Dev

How is a transducer different from a partially applied function?

From Dev

How to define function signatures partially in Haskell?

From Dev

How to enable a curried function to be inherited and then partially applied

From Dev

How to define function arguments partially? Python

From Dev

How to partially apply case class with type parameter in Scala

From Dev

Apply partially instantiated lemma

From Dev

r function in function arguments + apply

Related Related

  1. 1

    How do I partially apply an infix function like Basics.+?

  2. 2

    How do you partially apply a function with multiple arguments?

  3. 3

    Partially apply function n times

  4. 4

    How to efficiently apply a function to each DataFrame of a Pandas Panel

  5. 5

    How to apply my own function with r::apply()

  6. 6

    How to partially (re)apply bindings to descendants with knockout?

  7. 7

    Efficiently apply function to neuron outputs then sum, not apply function to sum

  8. 8

    How to write this function efficiently?

  9. 9

    How to apply a function that creates columns to dataframe in R

  10. 10

    How to apply a function to factored subgroups in R?

  11. 11

    How to apply function by groups in array in R?

  12. 12

    How to apply a function to several variables in R?

  13. 13

    How to efficiently apply a two variable function to data.frame( or matrix) elements - to fill an output matrix?

  14. 14

    Clojure - more idiomatic to return a closure, or partially apply the function?

  15. 15

    How to efficiently apply a regex substitution on a Moose attribute?

  16. 16

    J: How to efficiently apply a verb to prefixes of suffixes?

  17. 17

    How to nest quantile() function within apply() function in R or RStudio

  18. 18

    How to pass another function as a variable to apply function in R?

  19. 19

    R - function, matrix, apply

  20. 20

    R: Custom function in apply()

  21. 21

    How to efficiently pass function through?

  22. 22

    How to use the strip() function efficiently

  23. 23

    How is a transducer different from a partially applied function?

  24. 24

    How to define function signatures partially in Haskell?

  25. 25

    How to enable a curried function to be inherited and then partially applied

  26. 26

    How to define function arguments partially? Python

  27. 27

    How to partially apply case class with type parameter in Scala

  28. 28

    Apply partially instantiated lemma

  29. 29

    r function in function arguments + apply

HotTag

Archive