[R] Outer, kronecker, etc.

From: Stavros Macrakis <macrakis_at_alum.mit.edu>
Date: Wed, 12 Nov 2008 12:51:26 -0500


`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



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 12 Nov 2008 - 17:55:01 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 12 Nov 2008 - 20:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive