문제

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?

도움이 되었습니까?

해결책

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}

다른 팁

Another solution to consider is to use a modified addNA to allow it to output the factor level as a string in the first place:

addNA2 <- function (x, ifany = FALSE, as.string = TRUE)
{
    if (!is.factor(x)) 
        x <- factor(x)
    if (ifany & !any(is.na(x))) 
        return(x)
    ll <- levels(x)
    if (!any(is.na(ll))) 
        ll <- c(ll, NA)
    x <- factor(x, levels = ll, exclude = NULL)
    if(as.string) levels(x)[is.na(levels(x))] <- "NA"
    x
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top