[R] Interaction and factor ':'

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Nov 16 11:46:07 CET 2006


On Thu, 16 Nov 2006, Hong Ooi wrote:

> Not sure if this counts as a bug or not, but I just noticed in R 2.4.0
> that : and interaction are not quite equivalent. For example:

Well, that depends on what you mean by 'equivalent', a word that is not 
used in the help you quote but is used in help(":").  If you view a 
factor as an enumeration type, they are equivalent.

This difference seems to be long-standing (it was there in R 2.0.0), and 
I'd say it was unclear documentation which I will amend.

(As a historical note, interaction() behaves the same as S, whereas ':' in 
S does not have this alternative meaning but had been that way in R for 
many years.)

>
>> x <- factor(letters[1:4])
>> y <- factor(letters[11:14])
>
>> x:y
> [1] a:k b:l c:m d:n
> Levels: a:k a:l a:m a:n b:k b:l b:m b:n c:k c:l c:m c:n d:k d:l d:m d:n
>
>> interaction(x, y, sep=":")
> [1] a:k b:l c:m d:n
> Levels: a:k b:k c:k d:k a:l b:l c:l d:l a:m b:m c:m d:m a:n b:n c:n d:n
>
> The ordering of the levels is different between the two, although
> ?interaction says
>
>
>    f:g is the same as interaction(f,g, sep=":") when f and g are
> factors.
>
> I came across this when fitting multinom models, and the column order of
> the predicted probabilities shifted around depending on whether I used
> interaction or : on the LHS of the formula.

Which seems fine, as it is the labels and not the order which mattered.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list