# [R] Applying functions to partitions

Stavros Macrakis macrakis at alum.mit.edu
Tue Feb 17 04:42:32 CET 2009

```On Mon, Feb 16, 2009 at 7:52 PM, Bert Gunter <gunter.berton at gene.com> wrote:
> I suppose the clean way to do this would be to define a cartesian product of
> two factors with the induced lexicographic order (is there a standard
> function for doing this?):"
>
> Of course. ?interaction.

Perhaps my specification was unclear, because interaction does not do this:

g1 <- factor(c("b","a","c"),levels=c("b","a","c"),ordered=TRUE)
g2 <- factor(c(3,10,1,10), levels=c(3,10,1),ordered=TRUE)

interaction(g1,g2,lex.order=TRUE)
[1] b.3  a.10 c.1  b.10
Levels: b.3 b.10 b.1 a.3 a.10 a.1 c.3 c.10 c.1
Warning message:
In ans + ll * if1 :
longer object length is not a multiple of shorter object length

First problem: it calculates the cartesian product of the *levels*,
not of the *values*, so ignores duplicate values, which are important.
Second problem: it reorders them alphabetically, ignoring the original orders.
Third problem: it gives a warning if the number of levels is not the same.

expand.grid is closer, but it produces the cartesian product as a data
frame, not a factor, and the first argument varies fastest.  Using
expand.grid, I suppose you could do:

`*.factor` <- function(f1,f2)
{ val <- do.call(paste,rev(expand.grid(f2,f1)))
factor(val, levels=unique(val), ordered=TRUE) }

or

`*.factor` <- function(f1,f2)
factor(  do.call(paste,rev(expand.grid(f2,f1))),

levels=do.call(paste,rev(expand.grid(unique(f2),unique(f1))))
ordered=TRUE)

but to my eye those hardly look nicer than the code I gave before
which doesn't use expand.grid.

Is there something I'm missing here?

-s

```