Skip to content

Outer with Three Vectors

1 message · Liaw, Andy

#
Here's my shot at it.  The gouter function can be enhanced further (e.g., as
outer() does with dimnames), but I think the basic functionality is there.
You can basically pass in any number of vectors you want, but you need to
wrap them in a single list.  outer() allows arrays, but gouter() below will
only work with list of vectors.

gouter <- function(x, FUN, ...) {
  xgrid <- as.list(do.call("expand.grid", x))
  names(xgrid) <- NULL
  xdim <- sapply(x, length)
  array(do.call(deparse(substitute(FUN)), c(xgrid, list(...))),
                dim=sapply(x, length), dimnames=x)
}

Here's a simple test:
, , 6

   4  5
1 11 12
2 12 13
3 13 14

, , 7

   4  5
1 12 13
2 13 14
3 14 15

, , 8

   4  5
1 13 14
2 14 15
3 15 16

, , 9

   4  5
1 14 15
2 15 16
3 16 17

HTH,
Andy
------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}