[R] Syntax Check: rshape2 melt() [RESOLVED]

Rich Shepard rshepard at appl-ecosys.com
Fri Oct 28 18:49:48 CEST 2011


On Fri, 28 Oct 2011, Rich Shepard wrote:

>  May not be until Monday that I return with results on this effort as I'm
> going to be carfully checking and documenting each step and its results.

Dan, et al.:

   Got it working properly now. Took a different approach after re-reading
the source data into an R data frame.

   I applied the subset() function to extract all rows for a single stream
(all commands are on a single line in emacs but wrap when copied into
alpine):

> burns <- subset(chemdata, stream == 'BurnsCrk', select = c(site, sampdate,
param, quant), drop = T)

   Then I applied the reshape2 melt() function:

> burns.melt <- melt(burns, id.vars = c('site', 'sampdate', 'param'))

which produced this data frame:

> head(burns.melt)
     site   sampdate param variable  value
1 BC-0.5 1996-06-02    OH    quant     NA
2 BC-0.5 1996-06-02   SO4    quant 194.00
3 BC-0.5 1996-06-02   TDS    quant 530.00
4 BC-0.5 1996-06-02   TSS    quant     NA
5 BC-0.5 1996-04-19    As    quant   0.01
6 BC-1.5 1996-09-19    As    quant     NA

   Then the reshape2 dcast() function produced the desired results:

> burns.cast <- dcast(burns.melt, site + sampdate ~ param)
> head(burns.cast)
     site   sampdate Acid Ag   Al Alk-HO Alk-Tot   As   Ba Be Bo CO3    Ca Cd
1 BC-0.5 1996-04-19   NA NA 0.07     NA     162 0.01 0.18 NA NA  NA 76.56 NA
2 BC-0.5 1996-05-21   NA NA   NA     NA     152   NA   NA NA NA  NA    NA NA
3 BC-0.5 1996-06-02   NA NA   NA     NA     212   NA   NA NA NA  NA    NA NA
4   BC-1 1988-06-26   NA NA   NA     NA      NA 0.00   NA NA NA  NA    NA NA
5   BC-1 1988-07-30   NA NA   NA     NA      NA 0.00   NA NA NA  NA    NA NA
6   BC-1 1989-05-15   NA NA   NA     NA      NA 0.00   NA NA NA  NA    NA NA

  et cetera.

   Thank you all for your patient help. Now I should be able to produce
scatter plots and run linear regressions on selected parameter pairs by site
and date.

Carpe weekend, all,

Rich



More information about the R-help mailing list