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

mfrumin michael at frumin.net
Wed Jul 30 19:41:28 CEST 2008


I want the column that is never going to actually have the '(all)' level to
not become of type factor.  continuing the example:

chick_m$diet = as.integer(as.character(chick_m$diet))
is.factor(chick_m$diet) #returns FALSE
is.factor(cast(subset(chick_m, time == 0), diet + chick ~ time, mean,
margins="diet")$diet) #returns TRUE

does that make sense?  the way it works now, it totally screws things up
when the column for which you get margins is not a factor.  in my case, a
date column.

thanks,
Mike



hadley wrote:
> 
> 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/
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
View this message in context: http://www.nabble.com/bug-in-%27margins%27-behavior-in-reshape---cast-tp18737649p18738699.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list