[Rd] A "stack" function
Douglas Bates
bates@stat.wisc.edu
29 Mar 2000 10:13:45 -0600
I am using R in my undergraduate engineering statistics courses, in
which I am currently discussing one-way analysis of variance. Many of
the data sets for the exercises or examples give the data in the form
on one column per treatment level. To use the aov or lm functions
such data should be converted to one column with all the response
observations and a companion column with indicators of the treatment
level.
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.
Here is an example
> library(Devore5)
> data(ex10.09)
> boxplot(ex10.09)
> str(ex10.09) # show the structure
`data.frame': 6 obs. of 4 variables:
$ Wheat : num 5.2 4.5 6 6.1 6.7 5.8
$ Barley: num 6.5 8 6.1 7.5 5.9 5.6
$ Maize : num 5.8 4.7 6.4 4.9 6 5.2
$ Oats : num 8.3 6.1 7.8 7 5.5 7.2
> stack <- function(data)
+ data.frame(values = unlist(data),
+ lev = factor(rep(names(data), lapply(data, length))))
> stack(ex10.09) # try it on the example
values lev
Wheat1 5.2 Wheat
Wheat2 4.5 Wheat
Wheat3 6.0 Wheat
Wheat4 6.1 Wheat
Wheat5 6.7 Wheat
Wheat6 5.8 Wheat
Barley1 6.5 Barley
Barley2 8.0 Barley
Barley3 6.1 Barley
Barley4 7.5 Barley
Barley5 5.9 Barley
Barley6 5.6 Barley
Maize1 5.8 Maize
Maize2 4.7 Maize
Maize3 6.4 Maize
Maize4 4.9 Maize
Maize5 6.0 Maize
Maize6 5.2 Maize
Oats1 8.3 Oats
Oats2 6.1 Oats
Oats3 7.8 Oats
Oats4 7.0 Oats
Oats5 5.5 Oats
Oats6 7.2 Oats
> fm1 <- lm(values ~ lev, stack(ex10.09))
> anova(fm1)
Analysis of Variance Table
Response: values
Df Sum Sq Mean Sq F value Pr(>F)
lev 3 8.9833 2.9944 3.9565 0.02293 *
Residuals 20 15.1367 0.7568
---
Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1
--
Douglas Bates bates@stat.wisc.edu
Statistics Department 608/262-2598
University of Wisconsin - Madison http://www.stat.wisc.edu/~bates/
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._