[R] Supplying NA or Zeros in dataframe for missing factor combinations

Karsten Wolf wolf at uni-bremen.de
Thu May 14 23:28:41 CEST 2009

Hi there,
I am not getting anywhere near a solution, so here is my question. I  
have searched the archives and didn't find a solution, but maybe my  
search didn't use the right words. So here it is:

I have a dataframe with the following structure

factor1	factor2	value

In the (rather large) dataset I have to work on, there are some factor  
combinations missing, e.g.

factor1 <- c("2005", "2007", "2009)
factor2 <- c("Area1", "Area2", "Area3")

So in a perfect world there should be values for each combination in  
the dataset, eg values for "2005" in "Area1", "Area2" and "Area3", for  
"2007" and the three areas and so on.

But in my dataset some combinations are missing, e.g. a value for  
"2009" and "Area3".

How can I add complete my dataset with NA or 0 for the missing factor  

Is there a way to use unstack or do I have to create the combinations  
first and then search for each combination within the dataframe and  
use rbind to add a row with the missing combination and a value of NA  
or 0?


Karsten D. Wolf
Universität Bremen

More information about the R-help mailing list