如何让addNA和xtable一起工作?

发布于 2024-11-03 19:20:35 字数 384 浏览 1 评论 0原文

我正在使用 xtabs 来对一些包含 NA 的数据进行制表。为了确保总数完整,我使用 addNA 来计算缺少因子水平的数量。

然而,当使用xtable导出到LaTeX进行Sweaving时,这会导致问题,因为现在行和列名称中有NA。我有一个解决方案:

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

但这对于很多表来说可能会变得令人厌烦,有没有一种方法可以更自动地执行此操作?或者是否有更好的方法来生成表格?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红墙和绿瓦 2024-11-10 19:20:35

有趣的问题。我也找不到使用 xtable 本身来处理这个问题的方法。因此,我最好的建议是将您的解决方法变成一个可以轻松调用的小函数。

例如:

# 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)

这会创建有效的乳胶而不会出现错误:

% 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}

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}
数理化全能战士 2024-11-10 19:20:35

另一个需要考虑的解决方案是使用修改后的 addNA 来允许它首先将因子级别作为字符串输出:

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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文