# [R] short puzzles

Wiener, Matthew matthew_wiener at merck.com
Fri Jul 11 14:49:30 CEST 2003

```I'll try #1.  You can use outer twice:

outer(outer(x,y, "/"),z,function(a,b),"^")

In general I suppose you could write an "outer.n" function that would take
the vectors and the functions to be applied and loop through them.  And I
think that outer will then take care of the dimnames for you.  (Taking care
of #2, and, I think, #3.)

Hope this helps.

Matt Wiener

-----Original Message-----
From: Marc Vandemeulebroecke [mailto:vandemem at gmx.de]
Sent: Friday, July 11, 2003 7:48 AM
To: r-help at stat.math.ethz.ch
Subject: [R] short puzzles

Dear R users,

can someone help with these short puzzles?

1) Is there a function like outer() that evaluates a three-argument function
on a threedimensional grid - or else how to define such a function, say,
outer.3()? E.g., calculate (x/y)^z on (x,y,z) element of {1,2,3}x{3,4}x{4,5}
and
return the results in a 3-dimensional array. I would naively use outer() on
two of the arguments within a for() loop for the third argument and somehow
glue the array together. Is there a better way? What about outer.4(), or
even
outer.n(), generalizing outer() to functions with an arbitrary number of
arguments?

2)
Define a function dimnames.outer() such that dimnames.outer(x, y, "*")
returns, for x <- 1:2, y <- 2:3, the following matrix:

y
x   2 3
1 2 3
2 4 6

(Or does such such a function already exist?)

3)

How to combine puzzle 1 and puzzle 2? A function dimnames.outer.n() would be
a nice little tool.

4)

How can I access, within a function, the name of a variable that I have
passed to the function? E.g., letting a <- 2, and subsequently calling
function
f(a) as defined below,

f <- function (x) {
# How can I get "a" out of x?
}

5)

Finally: Letting x <- 2, how can I transform "x+y" into "2+y" (as some
suitable object), or generally "func(x,y)" into "func(2,y)"?

Many thanks,
Marc

--

Jetzt ein- oder umsteigen und USB-Speicheruhr als Prämie sichern!

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help

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

```