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))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top