Kevin Wright
2005-Oct-24 17:45 UTC
[R] How to create a new data.frame with all possible observations
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]]