From deepayan.sarkar at gmail.com  Thu Nov 30 22:53:48 2006
From: deepayan.sarkar at gmail.com (Deepayan Sarkar)
Date: Thu, 30 Nov 2006 13:53:48 -0800
Subject: [Rd] order of levels in interaction()
In-Reply-To: <Pine.LNX.4.64.0611302146210.19267@gannet.stats.ox.ac.uk>
References: <eb555e660611301325o3f255372t4bced4bd0c51e52d@mail.gmail.com>
	<Pine.LNX.4.64.0611302146210.19267@gannet.stats.ox.ac.uk>
Message-ID: <eb555e660611301353w731fd7d9sac960b0b601dbc0d@mail.gmail.com>

On 11/30/06, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
> On Thu, 30 Nov 2006, Deepayan Sarkar wrote:
>
> > Hi,
> >
> > ?interaction says:
> >
> > See Also:
> >
> >     'factor'; ':' where 'f:g' is the similar to 'interaction(f, g,
> >     sep=":")' when 'f' and  'g' are factors.
> >
> > This is not quite true (or maybe 'similar' is the operative word), since:
> >
> >> f <- factor(1:3); g <- factor(letters[1:3])
> >> levels(f:g)
> > [1] "1:a" "1:b" "1:c" "2:a" "2:b" "2:c" "3:a" "3:b" "3:c"
> >> levels(interaction(f, g, sep = ":"))
> > [1] "1:a" "2:a" "3:a" "1:b" "2:b" "3:b" "1:c" "2:c" "3:c"
> >
> > Note the different order of the levels. I like the order in f:g better
> > (and at least one other person agrees [1] ), but I also want the
> > flexibility of having a 'sep' argument. Would it be possible to change
> > interaction to allow this? A simple fix that makes interaction behave
> > like ":" is to replace
>
> No, for it also says
>
>       The levels are ordered so the level of the first factor varies
>       fastest, then the second and so on.  This is the reverse of
>       lexicographic ordering, and differs from ':'.  (It is done this
>       way for compatibility with S.)
>
> and reversing it would break things.  (It does, I tried it a while back.)

Ah, I should have read the help page more carefully.

I'll just write a custom version for my use, which is a fairly simple case.

-Deepayan

> > as.vector(outer(l, lvs, paste, sep = sep))
> >
> > with
> >
> > as.vector(t(outer(l, lvs, paste, sep = sep)))
> >
> > in interaction.
> >
> > -Deepayan
> >
> > [1] http://tolstoy.newcastle.edu.au/R/help/06/08/32638.html



[Rd] order of levels in interaction()
Deepayan Sarkar