The unstack function is a bit clumsy for this problem: library(MASS) cbind(Treatment = unstack(Rabbit, Treatment ~ Animal)[,1], Dose = unstack(Rabbit, Dose ~ Animal)[,1], unstack(Rabbit, BPchange ~ Animal))
A much better choice would be to use the reshape package: require(reshape) recast(Rabbit, Treatment + Dose ~ Animal, measure.var="BPchange") Kevin Wright On Sun, Nov 15, 2009 at 1:34 PM, Tariq Perwez <tariq.per...@gmail.com>wrote: > Hi Everyone, > > I am trying to understand the unstack() function but after struggling for > two days, I have given up. More specifically, I am trying the exercises at > the end of Chapter 1 of Data Analysis and Graphics Using R by Maindonald > and Braun, 2nd ed. Exercise 18 (p. 41) asks to unstack the Rabbit data > frame > from the MASS package to get a certain data frame that is shown in the > exercise. Authors suggest to use the unstack() three times but I am so new > to R that I have absolutely no clue as to what is to be done each of those > times. Sadly for me, the help page for unstack() does not give much help > either. For example, the statement in the help page regarding the argument > form, "a two-sided formula whose left side evaluates to the vector to be > unstacked and whose right side evaluates to the indicator of the groups to > create" is very cryptic to me. Basically, I have tried things like: > > unstack(Rabbit, Dose ~ Animal) > > but notice that what I get is a data frame in which other columns of the > Rabbit data frame disappear. I would appreciate if someone could help me > understand this function. On page 17 of the same book, there is an example > of unstack() function but that one uses a very simple data frame (only two > columns). I would like to know how to handle more complex data frames as in > the exercise. BTW, this is not a school assignment; I am learning R using > this book on my own. Thanks for any help. Regards, > > Tariq > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- Kevin Wright [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.