[R] Syntax Check: rshape2 melt()

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Fri Oct 28 02:10:12 CEST 2011


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Rich Shepard
> Sent: Thursday, October 27, 2011 4:29 PM
> To: r-help at r-project.org
> Subject: Re: [R] Syntax Check: rshape2 melt()
> 
> On Thu, 27 Oct 2011, Nordlund, Dan (DSHS/RDA) wrote:
> 
> > It appears that what you did differently was to dcast the un-melted
> data.
> > In addition, you shouldn't need to turn your dates into factors.  I
> assume
> > that your unaltered data is in tds.anal.
> 
> Dan,
> 
>    It's been that sort of a day when that might well have been what I
> did. So
> I checked and re-melted the data with similar lack of results.
> 
> > Melt the data this way:
> >
> >> m.tds.anal <-  melt(tds.anal, id.vars = c('site', 'sampdate',
> 'param'))
> 
>    OK. This yields:
> 
> head(m.tds.anal)
>       site   sampdate param variable value
> 1   UDS-O 2006-12-06   TDS    quant 10800
> 2  STC-FS 1996-06-14  Cond    quant   280
> 3   UDS-O 2007-10-04    Mg    quant  1620
> 4   UDS-O 2007-10-04   SO4    quant  7580
> 5 JCM-10B 2007-06-21    Ca    quant    79
> 6 JCM-10B 2007-06-21    Cl    quant   114
> 
> > Then, cast the data this way:
> >> wanted <- dcast(m.tds.anal, site + sampdate ~ param)
> > Does this do what you wanted?
> 
>    I still get this error:
> 
> wanted <- dcast(m.tds.anal, site + sampdate ~ param)
> Aggregation function missing: defaulting to length
> 
> and the values are replaced by lengths in the output:
> 
> head(wanted)
>      site   sampdate Ca Cl Cond Mg Na SO4 TDS
> 1 BC-0.5 1996-04-19  1  1    1  1  0   1   1
> 2 BC-0.5 1996-05-21  0  0    1  0  0   1   1
> 3 BC-0.5 1996-06-02  0  0    1  0  0   1   1
> 4   BC-1 1994-05-24  1  1    1  1  1   1   1
> 5   BC-1 1994-06-02  1  1    1  1  1   1   1
> 6   BC-1 1994-06-10  1  1    1  1  1   1   1
> 
>    This is what happens each time I try. That's why I thought that
> sampdate
> needed to be a factor rather than a date. I certainly have no idea why
> I'm
> not getting valid results, but appreciate the help that you and others
> provide so I can learn to do this correctly.
> 
> Thanks again,
> 
> Rich
> 

Rich,

I will be leaving work shortly, and won't be able to look at this again until tomorrow some time.  However, I went back and reread the help page on dcast.  Here is part of that page.

fun.aggregate 

aggregation function needed if variables do not identify a single observation for each output cell. Defaults to length (with a message) if needed but not specified.

It looks like your data may be slightly different than I expected.  The fact that dcast is looking for an aggregation  function suggests that you have more than one record for some combination of site|sampdate|param.  No param value ('TDS', 'SO4', ...) should occur more than once for any combination of site and sampdate (i.e. no multiple measures of the same parameter on the same day in the same site).  So, you might want to check your data to make sure you have no more than 1 measurement per param per sampdate per site.

If that isn't the problem, then I would start thinking about restarting R and then re-installing reshape2.  Good luck.

Dan 

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204




More information about the R-help mailing list