Zheyuan Li
Zheyuan Li

Reputation: 73265

How to use "lapply" and "outer" together?

A very short question:

X <- data.frame(x = runif(10), y = runif(10))
lapply(X, outer, Y = 0:2, FUN = "^")

The FUN should really be passed to outer as an additional parameter, but R will assume it is the function fed to lapply.

Is there anyway to get around this, besides

fun <- function (x) outer(x, 0:2, "^")
lapply(X, fun)

or

vecX <- split(outer(unlist(X), 0:2, "^"), gl(2, 10, labels = names(X)))
lapply(vecX, matrix, nrow = nrow(X))

Upvotes: 2

Views: 3491

Answers (1)

Zheyuan Li
Zheyuan Li

Reputation: 73265

Looks like I am getting stupid after being less active in answering questions... Just pass everything in by position, to disable argument matching by names.

lapply(X, outer, 0:2, "^")

Upvotes: 2

Related Questions