Linens
Linens

Reputation: 7942

Passing two parameter functions into apply

Say I have a function called

myfun <- function(x,y) {median(x,y)} # obviously the actual function is 
                                     # something more complicated

Now lets say in a certain use, the y parameter is constant, (say c(1,2,3,4,5)). Is there any way I can pass this into apply without wrapping it in another function? i.e.
instead of

apply(mydf, 2, function(x) myfun(x, c(1,2,3,4,5)))

to pass something like

apply(mydf, 2, myfun(,(c(1,2,3,4,5))))

This is purely cosmetic and I know it won't make much difference to the running time. I just want to know if an option like this is possible because wrapping my function in a function each time seems inefficient

Upvotes: 4

Views: 2820

Answers (1)

Paul Hiemstra
Paul Hiemstra

Reputation: 60924

I think this should work:

apply(mydf, 2, myfun, y = c(1,2,3,4,5))

Remains untested as I can't access R right now.

Upvotes: 8

Related Questions