Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing two parameter functions into apply

Tags:

r

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

like image 593
LostLin Avatar asked Feb 22 '23 03:02

LostLin


1 Answers

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.

like image 147
Paul Hiemstra Avatar answered Mar 02 '23 18:03

Paul Hiemstra