[R] Syntax Check: rshape2 melt()

Rich Shepard rshepard at appl-ecosys.com
Thu Oct 27 19:22:25 CEST 2011


On Thu, 27 Oct 2011, Nordlund, Dan (DSHS/RDA) wrote:

> What I think you want is as simple as
> test.melted <- melt(test)

Dan,

   I see the difference this syntax makes; sampdate is the variable
associated with the value (quant) for a specific ID pair of site and param.

> wanted <- cast(test.melted, site + sampdate ~ param)

   This, unfortunately, doesn't work:

c.tds.anal <- cast(m.tds.anal, site + sampdate ~ param)
Error: could not find function "cast"

   ?cast has an example using acast() so I tried that:

c.tds.anal <- acast(m.tds.anal, site + sampdate ~ param)
Error in eval(expr, envir, enclos) : object 'sampdate' not found

   Changing 'sampdate' to 'variable' produces results, but not what's needed:

> c.tds.anal <- acast(m.tds.anal, site + variable ~ param)
Aggregation function missing: defaulting to length
> head(c.tds.anal)
                 Ca Cl Cond Mg Na SO4 TDS
BC-0.5_sampdate  1  1    3  1  0   3   3
BC-0.5_quant     1  1    3  1  0   3   3
BC-1_sampdate    8  8   11  8  7  10  10
BC-1_quant       8  8   11  8  7  10  10
BC-1.5_sampdate  3  4    6  3  0   6   6
BC-1.5_quant     3  4    6  3  0   6   6

   Now that I better understand melt(), I'll read ?cast.

Thanks very much,

Rich



More information about the R-help mailing list