[R] ellipsis question

Setzer.Woodrow@epamail.epa.gov Setzer.Woodrow at epamail.epa.gov
Tue Mar 26 23:07:57 CET 2002

```I think expand.grid() should do what you want pair() and ntuple() to do.

R. Woodrow Setzer, Jr.                                            Phone:
(919) 541-0128
Experimental Toxicology Division                       Fax:  (919)
541-5394
Pharmacokinetics Branch
NHEERL MD-74; US EPA; RTP, NC 27711

Robin Hankin
<r.hankin at auckland.a        To:     r-help at stat.math.ethz.ch
c.nz>                       cc:
Sent by:                    Subject:     [R] ellipsis question
owner-r-help at stat.ma
th.ethz.ch

03/26/02 04:18 PM
r.hankin

Hello R experten

I have just written a little function to calculate all pairwise
combinations of two vector arguments:

> pair(c(1,2,3),c(7,8))
[,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>

> ntuple(c(1,2,3),c(7,8),c(14,15))
[,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.

===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
}

--

Robin Hankin, Lecturer,
School of Geographical and Environmental Science
Private Bag 92019 Auckland
New Zealand

r.hankin at auckland.ac.nz
tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042

as of: Wed Mar 27 09:15:00 NZST 2002
This (linux) system up continuously for:  209 days, 16 hours, 10 minutes
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-.-
http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._._._

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```