[R] Supplying NA or Zeros in dataframe for missing factorcombinations
Peter Alspach
PAlspach at hortresearch.co.nz
Fri May 15 02:15:20 CEST 2009
Tena koe Karsten
Does your dataframe contain multiple values for the same factor combination? If not, then using expand.grid(), followed by merge() should work.
If your dataframe is DF, then the following untested code should do it:
fullDF <- expand.grid(factor1=levels(DF$factor1), factor2=levels(DF$factor2))
fullDF <- merge(fullDF, DF, all.x=T)
HTH ....
Peter Alspach
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Karsten Wolf
> Sent: Friday, 15 May 2009 9:29 a.m.
> To: r-help at r-project.org
> Subject: [R] Supplying NA or Zeros in dataframe for missing
> factorcombinations
>
> 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 combinations?
>
> 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?
>
> Thanks,
> karsten
>
> ---
> Karsten D. Wolf
> Universität Bremen
> http://www.ifeb.uni-bremen.de/wolf
>
> ______________________________________________
> R-help at 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.
>
The contents of this e-mail are confidential and may be subject to legal privilege.
If you are not the intended recipient you must not use, disseminate, distribute or
reproduce all or any part of this e-mail or attachments. If you have received this
e-mail in error, please notify the sender and delete all material pertaining to this
e-mail. Any opinion or views expressed in this e-mail are those of the individual
sender and may not represent those of The New Zealand Institute for Plant and
Food Research Limited.
More information about the R-help
mailing list