[R] How to create a new data.frame with all possible observations

From: Kevin Wright <kwright68_at_gmail.com>
Date: Tue 25 Oct 2005 - 03:45:26 EST


I would like to use something like expand.grid to get a data.frame with all possible combinations of model terms from a formula. For example:

R> dat=data.frame(y=rnorm(8),
+ trt=rep(c("A","B"),4),
+ state=c(rep(c('IA','NE'),each=4)),
+ county=c('P','P','S','S','J','J','N','N'))
R> dat=dat[-1,]
R> dat
y trt state county

2 0.36 B IA P
3 0.44 A IA S
4 2.26 B IA S

5 -0.12 A NE J
6 0.84 B NE J
7 -2.11 A NE N
8 -0.69 B NE N

If my model is
model=formula(y~trt+state + county%in%state)

Then I would like to create a data.frame that includes the missing observation for
A + IA + P %in IA (trt + state + county %in% state)

I currently use expand.grid to get all possible combinations of variables, then delete impossible combinations like "J %in IA". This works, but is a bit clumsy and I wonder if there is a more general solution.

Ideas welcome.

Kevin Wright

        [[alternative HTML version deleted]]



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Oct 25 03:52:10 2005

This archive was generated by hypermail 2.1.8 : Tue 25 Oct 2005 - 05:17:26 EST