The problem is quite simple yet I can't find the answer.
I have myfun <- function(x, y)
. How can I sapply
this function over a list of y
?
To apply over x
I would do this
iterables <- 1:10
sapply(iterables, myfun, y)
But I want the iterables to be y instead.
You have several options - e.g. one mentioned by sgibb which relies on how R interprets function arguments, i.e. that myfun(y, x = x)
is the same as myfun(x, y)
.
I prefer creating anonymous functions since it's easier to understand what's happening:
sapply(iterables, function(iter) myfun(x, iter))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With