Skip to content
Prev 18258 / 398503 Next

ellipsis question

Hello R experten

I have just written a little function to calculate all pairwise
combinations of two vector arguments:
[,1] [,2]
[1,]    1    7
[2,]    1    8
[3,]    2    7
[4,]    2    8
[5,]    3    7
[6,]    3    8
I want to generalize this to any number of arguments, for example,

 <fantasy>
[,1] [,2] [,3]
[ 1,]    1    7   14
[ 2,]    1    8   14
[ 3,]    2    7   14
[ 4,]    2    8   14
[ 5,]    3    7   14
[ 6,]    3    8   14
[ 7,]    1    7   15
[ 8,]    1    8   15
[ 9,]    2    7   15
[10,]    2    8   15
[11,]    3    7   15
[12,]    3    8   15

</fantasy>

One object is to have b <- c(0,1) and then ntuple(b,b,b,b,b,b,b,b)
will generate all 256 distinct bytes.

I tried to use the ellipsis argument "..." with no success (because it
takes only named arguments).  Any help anyone?  While I'm writing, my
meshgrid() below works as per octave's, but it's a bit clunky.  Is
there anything better?

This is the only thing I've found so far that Octave can do that I
can't easily replicate in R.

thanks in advance





===work done so far===




  meshgrid <- function(x,y) {
    ignore.arg2 <- function(x,y) {x}
    list(x=t(outer(x,y,FUN=ignore.arg2)),y=outer(y,x,FUN=ignore.arg2))
  }

                                                 
pair <- function(a,b) {
  len <- length(a) * length(b)
  jj <- meshgrid(a,b)
  cbind(as.vector(jj$x),as.vector(jj$y))
}


ntuple <- function(a,b, ...) {
#help help how do I do this
}