[R] reshape (a better way)

Chuck Cleland ccleland at optonline.net
Wed Jan 26 20:58:49 CET 2005


It's in the documentation for reshape - make the varying argument a 
list.  Here is an example:

famf.uni <- reshape(famf,
  varying = list(c("DUSI2BL",   "DUSI3M",    "DUSI6M",    "DUSI1Y"   ),
                 c("DNKTOTBL",  "DNKTOT3M",  "DNKTOT6M",  "DNKTOT1Y" ),
                 c("DAYDNKBL",  "DAYDNK3M",  "DAYDNK6M",  "DAYDNK1Y" ),
                 c("HVYALCBL",  "HVYALC3M",  "HVYALC6M",  "HVYALC1Y" ),
                 c("BALCSV15",  "DALCSV15",  "EALCSV15",  "FALCSV15" ),
                 c("BALCSV27",  "DALCSV27",  "EALCSV27",  "FALCSV27" ),
                 c("PCSBLT",    "PCS3MT",    "PCS6MT",    "PCS1YT"   ),
                 c("MCSBLT",    "MCS3MT",    "MCS6MT",    "MCS1YT"   ),
                 c("BSIANXBL",  "BSIANX3M",  "BSIANX6M",  "BSIANX1Y" ),
                 c("BSIPSYBL",  "BSIPSY3M",  "BSIPSY6M",  "BSIPSY1Y" ),
                 c("CESDBL",    "CESD3M",    "CESD6M",    "CESD1Y"   )),
  v.names = c("DUSI",   "DNKTOT",  "DAYDNK",
               "HVYALC", "ALCSV15", "ALCSV27", "PCST",
               "MCST",   "BSIANX",  "BSIPSY",
               "CESD"),
  timevar = "TIME", times = c("BL", "3M", "6M", "1Y"), direction = "long")

hope this helps,

Chuck Cleland

Jean Eid wrote:
> I am using the NLSY79 data (longitudinal data from the Bureau of labour
> stats in the US). The extractor exctracts this data in a "wide" format and
> I need to reshape it into a long format.
> 
> What I am doing right now is to do it in chuncks for each and evry
> variable that is varying and then I merge the data together. This is
> taking a long time. my question is:
> 
> How do I specify that there are multiple variables that are varying in
> reshape. Is there a way to do this?
> 
> The idea is to have something like varying1, varying2,etc,... and each are
> assosiated with their own times1, times2.
> 
> something like
> reshape(mydata, direction="long", varying1=varying1,varying2=varying2,
> varying3=varying3, split=list(regexp="[a-z][0-9]", include=TRUE),
> idvar="ID", times1=times1, times2=times2, times3=times3)

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 452-1424 (M, W, F)
fax: (917) 438-0894




More information about the R-help mailing list