Search code examples
rxtable

How to get addNA and xtable to work together?


I'm using xtabs to tabulate some data that contains NAs. In order to make sure the totals are complete, I'm using addNA to count the ones with missing factor levels.

However, this causes problems when using xtable to export to LaTeX for Sweaving because there are NAs in the row and column names now. I have a solution:

rownames(tab)[is.na(rownames(tab))]<-"NA"
colnames(tab)[is.na(colnames(tab))]<-"NA"

But this can become tiresome for lots of tables, is there a way of doing this more automatically? Or is there a better way of producing the tables in the first place?


Solution

  • Interesting question. I couldn't find a way of dealing with this using xtable itself, either. So the best I can suggest is to turn your workaround into a little function that can then be called easily.

    For example:

    # Construct some data
    df <- data.frame(
      x1 = addNA(sample(c(NA, LETTERS[1:4]), 100, replace = TRUE)),
      x2 = addNA(sample(c(NA, letters[24:26]), 100, replace = TRUE))
    )
    
    # Create a function to rename NA row and column names in a data.frame
    rename_NA <- function(x){
      rownames(x)[is.na(rownames(x))] <- "NA"
      colnames(x)[is.na(colnames(x))] <- "NA"
      x
    }
    
    tab <- rename_NA(xtabs(~x1+x2, data=df))
    xtable(tab)
    

    This creates valid latex without error:

    % latex table generated in R 2.13.0 by xtable 1.5-6 package
    % Wed Apr 27 17:20:21 2011
    \begin{table}[ht]
    \begin{center}
    \begin{tabular}{rrrrr}
      \hline
     & x & y & z & NA \\ 
      \hline
    A & 4.00 & 7.00 & 10.00 & 4.00 \\ 
      B & 6.00 & 5.00 & 4.00 & 2.00 \\ 
      C & 8.00 & 4.00 & 4.00 & 2.00 \\ 
      D & 8.00 & 5.00 & 1.00 & 6.00 \\ 
      NA & 5.00 & 2.00 & 7.00 & 6.00 \\ 
       \hline
    \end{tabular}
    \end{center}
    \end{table}