[R] margin.table

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jul 2 14:53:38 CEST 2001


On Mon, 2 Jul 2001, Jagat Sheth wrote:

> I was wondering if the following behaviour of margin.table in R 1.3.0 is normal.

Yes.

> > mm <- data.frame(x=c(0,0,1), y=c(1,1,0), z=c(0,1,0))
> > margin.table(mm,1)
> [1] 1 2 3
> <0 rows> (or 0-length row.names)
>
> while using 'apply(x,margin,sum)' I get
>
> > apply(mm,1,sum)
> 1 2 3
> 1 2 1
>
> which appears to be normal. Similar behaviour when margin=2.
>
> On the other hand, the following seems to be fine.
>
> > mmm <- as.matrix(mm)
> > margin.table(mmm,1)
> 1 2 3
> 1 2 1
> > margin.table(mmm,2)
> x y z
> 1 2 1

Try looking up the help page:

Arguments:

       x: an array


A data frame is *not* an array:

> is.array(mm)
[1] FALSE

and final line of margin.table tries to make the result make into a
data frame.

One could argue that as it accepted a non-array it should do a better job
with it, but we need the author (who is away this week) to explain why
he copied the class of the first argument to the result.

-- 
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