[R] bug in 'margins' behavior in reshape - cast

hadley wickham h.wickham at gmail.com
Wed Jul 30 19:02:39 CEST 2008


On Wed, Jul 30, 2008 at 11:47 AM, mfrumin <michael at frumin.net> wrote:
>
> according to the documentation of the cast function in the reshape function,
> I would expect this bit of code from the examples to calculate marginal
> means over only the 'diet' variable.
>
> #Chick weight example
> names(ChickWeight) <- tolower(names(ChickWeight))
> chick_m <- melt(ChickWeight, id=2:4, na.rm=TRUE)
> cast(chick_m, diet + chick ~ time, mean, margins="diet")
>
> But,
> summary(cast(subset(chick_m, time == 0), diet + chick ~ time, mean,
> margins="diet")) returns:
>
>    diet        chick          0
>  1    :21   (all)  : 4   Min.   :39.00
>  2    :11   18     : 1   1st Qu.:40.85
>  3    :11   16     : 1   Median :41.00
>  4    :11   15     : 1   Mean   :41.05
>  (all): 0   13     : 1   3rd Qu.:42.00
>            9      : 1   Max.   :43.00
>            (Other):45
>
> which shows that it calculated margins on the 'chick' column as well.  this
> behavior causes great problems for me when in my data one of the columns is
> a non-factor column, eg a Date, but the unrequested margining behavior turns
> that column into a factor.
>
> am I missing something?

Hi Mike,

I'm not sure what you want - if you want margins at the diet level,
then you have to margin over all of the chicks at that level, which is
then displayed as (all).  Maybe you want margin  = "grand_row" ?

Hadley


-- 
http://had.co.nz/



More information about the R-help mailing list