[R] reshape problem: id and variable names not being recognized

jim holtman jholtman at gmail.com
Mon Jan 26 17:57:59 CET 2009


Look at your 'str(mdat)' and you will see that there is not a variable
call 'mpg'; it is one of the levels of the 'variable'.

On Mon, Jan 26, 2009 at 11:38 AM, MW Frost <mwfrost at gmail.com> wrote:
> Hi everyone. Long time listener, first-time caller here.
>
> I have a data set that's been melted with the excellent reshape package, but
> I can't seem to cast it the way I need to.
>
> Here's the melted data's structure:
>
>> str(mdat)
> 'data.frame':   6978 obs. of  4 variables:
> $ VehType : Factor w/ 2 levels "Car","Truck": 1 1 2 1 1 2 1 1 1 1 ...
> $ Year    : Factor w/ 6 levels "2003","2004",..: 5 1 5 6 6 2 2 3 2 5 ...
> $ variable: Factor w/ 1 level "mpg": 1 1 1 1 1 1 1 1 1 1 ...
> $ value   : num  22.4 21.5 22.6 22.4 25 ...
>
> For the purpose of testing, I have stripped out all the variables except for
> "mpg."
> Casting it without specifying any ids or variables works fine:
>
>> cast(mdat,,mean)
>  VehType Year      mpg
> 1      Car 2003 22.03623
> 2      Car 2004 21.94160
> 3      Car 2005 21.77286
> 4      Car 2006 21.49105
> 5      Car 2007 21.38180
> 6      Car 2008 21.56873
> 7    Truck 2003 16.91461
> 8    Truck 2004 16.88771
> 9    Truck 2005 17.19801
> 10   Truck 2006 17.48225
> 11   Truck 2007 17.40694
> 12   Truck 2008 17.74042
>
> I should then be able to make a crosstab of the means by writing a formula,
> right? It fails, though:
>
>> cast(mdat, VehType ~ Year | mpg, mean)
> Error: Casting formula contains variables not found in molten data: mpg
>
> When I make the same table by using "variable" instead of the name of my
> variable, it works:
>
>> cast(mdat, VehType ~ Year | variable, mean)
> $mpg
>  VehType     2003     2004     2005     2006     2007     2008
> 1     Car 22.03623 21.94160 21.77286 21.49105 21.38180 21.56873
> 2   Truck 16.91461 16.88771 17.19801 17.48225 17.40694 17.74042
>
> Why can't it find the "mpg" variable when I call it explicitly?
>
> Thanks,
> Matt Frost
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list