Вопрос

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