Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Nov 29 09:38:18 CET 2003

On Sat, 29 Nov 2003 Bill.Venables at csiro.au wrote:

> Converting a collection of factors and a vector into an array is a general
> problem for which there is no general tool available (but it would be easy

It's called xtabs() in R:

allergy$fitted <- fitted(allergy.fit.main.2int)
xtabs(fitted ~ t + f + c, data=allergy)

and you permute the dimensions by reordering the factors.

Note that the roughly analogous crosstabs in S-PLUS will not do this, but
xtabs generates a call to table() which could be used directly.

In this case there were no repeated (and no missing) combinations of 
factors: if there has been just predict the model at all combinations and 
apply xtabs/table to the predictions.

> to write and curiously the S-PLUS function as.data.frame.array  does the
> inverse operation).  Let me think about it...
> In your case, however, the operation is easy, because you can exploit the
> regular layout of the data.
> not <- c("none", "one", "two")
> dm <- rep(3,3)
> dn <- list(c = not, f = not, t = not)
> Otable <- array(allergy$y, dim=dm, dimnames = dn)
> Etable <- array(fitted(allergy.fit.main.2int), dim = dm, dimnames = dn)
> Etable
> You just need to remember that the dimensions come out as c-rows, f-colums,
> t-layers.  If you want them in some other order the tool to use is aperm( ),
> but I'll leave that interesting story for you to sort out.
> Bill Venables.

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