[Rd] A "stack" function

Peter Dalgaard BSA p.dalgaard@biostat.ku.dk
29 Mar 2000 18:42:05 +0200


Thomas Lumley <thomas@biostat.washington.edu> writes:

> On 29 Mar 2000, Douglas Bates wrote:
> 
...
> > In Minitab this operation is called "stacking" columns and the command
> > to perform the operation is "stack".  I have written a similar function
> > for R.  Before taking the time to make it elegant and to document it,
> > I thought I would check if I have overlooked an existing way of doing
> > this.
> > 
> 
> It would be really nice if you could also do the reverse: take repeated
> measurements in 'long' form and convert them into 'wide' form.
>   In Stata both of these are available in the -reshape- command.  I don't
> know any really simple way of doing it in R.

Not really simple, but wouldn't something like
as.data.frame(tapply(x,f,as.vector)) do it? Hmmm... This seems to work;

> tt<-data.frame(c(tapply(ss$values,ss$lev,as.vector)))
> tt
  Barley Maize Oats Wheat
1    6.5   5.8  8.3   5.2
2    8.0   4.7  6.1   4.5
3    6.1   6.4  7.8   6.0
4    7.5   4.9  7.0   6.1
5    5.9   6.0  5.5   6.7
6    5.6   5.2  7.2   5.8

Re. stack(), wouldn't you want to stack over only a subset of columns,
while replicating all the remaining columns? There's some code in
subset() which might be used for selecting the columns.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._