[R] How to create unique factor from two factors? + Boostrap Q

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sun Nov 9 22:55:58 CET 2003


Prof Brian Ripley <ripley at stats.ox.ac.uk> writes:

> Well, it is one of those things
> 
> -- it works in R but not in S
> -- it appears in the examples for help(":") but is not otherwise mentioned
>    on the help page (why?)
> -- it does not give a numerical list of combinations, as asked for
> -- it does give unused levels, which in this application is disastrous.
> 
> so I at least do not find it `easier'.
> 
> > a <- factor(letters)[1:6]
> > b <- factor(rep(letters[1:3], each=2))
> > a:b
> [1] a:a b:a c:b d:b e:c f:c
> 78 Levels: a:a a:b a:c b:a b:b b:c c:a c:b c:c d:a d:b d:c e:a e:b e:c ... 

My preference is 

> interaction(a,b,drop=TRUE)
[1] a.a b.a c.b d.b e.c f.c
Levels: a.a b.a c.b d.b e.c f.c

This is in Splus too, is documented, and has the possibility of
excluding unused levels. (I'm fairly sure, btw, that we decided to
standardize the :/. thing, but it seems to have been forgotten). It
doesn't give a numerically labeled result, but did Scott really want
that? (as.integer or levels(x) <- seq(along=levels(x)) will get you
there soon enough).

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list