Skip to content
Prev 278727 / 398502 Next

Help with recast() syntax

Inline below...

 On Mon, 28 Nov 2011 21:32:21 -0800 (PST), Chris Conner
<connerpharmd at yahoo.com> wrote:
Thanks for the sample data.
I don't think you need to melt the data first, so you don't need the 
 recast function.

 # reshape2 is faster than reshape, but slightly syntactically different
 library(reshape2)
 # rename the RESULT levels
 DF0 <- DF
 levels( DF0$RESULT ) <- c( "NEG_TOTAL", "POS_TOTAL" )
 # cast to data frame, use sum if more than one row for a given YR_MO
 DF0 <- dcast( DF0, YR_MO~RESULT, sum, value.var="TOT_TESTS" )
 # The rest of this is to make the data frame look like your result, 
 which seems
 # unnecessary to me, but perhaps there is a good reason for keeping X 
 and RESULT
 DF1 <- merge( DF[ DF$RESULT=="POS", c( "X", "RESULT", "YR_MO" ) ], DF0 
 )
 DF2 <- DF1[,c("X", "RESULT", "YR_MO", "POS_TOTAL", "NEG_TOTAL" ) ]
(Please remember that this is a plain text email list.)

 ---------------------------------------------------------------------------
 Jeff Newmiller                        The     .....       .....  Go 
 Live...
 DCN:<jdnewmil_at_dcn.davis.ca.us>     Basics: ##.#.       ##.#.  Live 
 Go...
                                       Live:   OO#.. Dead: OO#..  
 Playing
 Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
 /Software/Embedded Controllers)               .OO#.       .OO#.  
 rocks...1k