[R] short puzzles

Marc Vandemeulebroecke vandemem at gmx.de
Fri Jul 11 13:47:44 CEST 2003


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!




More information about the R-help mailing list