[R] Outer, kronecker, etc.
Stavros Macrakis
macrakis at alum.mit.edu
Wed Nov 12 18:51:26 CET 2008
`outer` (and related functions like kronecker) require that their
functional argument operate elementwise on arrays. This means for
example that
outer( 1:2, 3:4, list)
or
outer(1:2,3:4,function(a,b){1})
gives an error.
Is there a version of `outer`/`kronecker`/etc. that takes arbitrary
functions and does its own elementwise mapping? In the first example
above, I'd expect the result to be the same as
mm <- matrix(list(list(1,3),list(1,4),list(2,3),list(2,4)),2,2)
which prints as
[,1] [,2]
[1,] List,2 List,2
[2,] List,2 List,2
By the way: how can I get this not to abbeviate the entries but
instead give me something like:
[,1] [,2]
[1,] list(1, 3) list(2, 3)
[2,] list(1, 4) list(2, 4)
The closest I can get is matrix(as.character(mm),2,2)
By-the-way^2: is there some Xapply function that maps a function over
all the elements of a structure (vector, matrix, list, ...) and
preserves the original structure? For example, I'd want
Xapply(matrix(1:4^2,2,2),sqrt) == sqrt(matrix(1:4^2,2,2)). In this
case, I'd use Xapply(as.character,mm), because as.character returns a
vector.
Thanks,
-s
More information about the R-help
mailing list