[R] Syntax Check: rshape2 melt()

Rich Shepard rshepard at appl-ecosys.com
Fri Oct 28 01:28:34 CEST 2011


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



More information about the R-help mailing list