[R] Two-way tables of data, etc

Prof Brian D Ripley ripley at stats.ox.ac.uk
Wed Sep 5 07:42:56 CEST 2001


On Wed, 5 Sep 2001, Murray Jorgensen wrote:

> Suppose I have a randomized block experiment with response vector y, treatment
> factor A, and block factor B. How may I print out the data in a nice two-way
> array?
>
> Here is one solution that I found (data from Steel And Torrie):
>
> block <- gl(4,6,24)
> tnames <- c("Seedling","Early", "Full", "Full/100",
>           "Ripening", "Uninoculated")
> treat <- gl(6,1,24,label=tnames)
> content <- scan()
> 4.4 3.3 4.4 6.8 6.3 6.4
> 5.9 1.9 4.0 6.6 4.9 7.3
> 6.0 4.9 4.5 7.0 5.9 7.7
> 4.1 7.1 3.1 6.4 7.1 6.7
>
> content <- content+30
> cmat <- matrix(content,nrow=4,ncol=6,byrow=T)
> dimnames(cmat) <- list(NULL,tnames)
> cmat
>
>
> OK, but what if I wanted to do something similar for a two-way array with
> multiple observations per cell? Or present the cell means? And what about
> displaying multi-way arrays of data?

xtabs(content ~ block + treat)

is almost what you gave, better labelled.

The obvious (to me) tools are xtabs and ftable to flatten arrays.


-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list