不像xtable那样在knitr :: kable输出中显示NA

时间:2017-09-10 11:50:52

标签: r knitr xtable kableextra kable

我想在NAs输出中省略knitr::kablextable相同的输出(请参阅下面的kablextable输出)。

fm12anova <-
  structure(list(SOV = c("Days", "Residuals"), Df2 = c(1L, 178L
  ), Sum.Sq2 = c(162702.651909532, 405251.617480465), Mean.Sq2 = c(162702.651909532, 
  2276.69448022733), F.value2 = c(71.4644205986244, NA), Pr..F.2 = c(9.89409632221545e-15, 
  NA), Sum.Sq1 = c(30031.0139578738, NA), Mean.Sq1 = c(30031.0139578738, 
  NA), NumDF1 = c(1L, NA), DenDF1 = c(17.0000010028433, NA), F.value1 = c(45.8530037127448, 
  NA), Pr..F.1 = c(3.26378804516025e-06, NA)), class = "data.frame", row.names = c(NA, 
  -2L), .Names = c("SOV", "Df2", "Sum.Sq2", "Mean.Sq2", "F.value2", 
  "Pr..F.2", "Sum.Sq1", "Mean.Sq1", "NumDF1", "DenDF1", "F.value1", 
  "Pr..F.1"))

library(knitr)
kable(fm12anova)


|SOV       | Df2|  Sum.Sq2|   Mean.Sq2| F.value2| Pr..F.2|  Sum.Sq1| Mean.Sq1| NumDF1| DenDF1| F.value1| Pr..F.1|
|:---------|---:|--------:|----------:|--------:|-------:|--------:|--------:|------:|------:|--------:|-------:|
|Days      |   1| 162702.7| 162702.652| 71.46442|       0| 30031.01| 30031.01|      1|     17|   45.853| 3.3e-06|
|Residuals | 178| 405251.6|   2276.694|       NA|      NA|       NA|       NA|     NA|     NA|       NA|      NA|

library(xtable)
xtable(fm12anova)
% latex table generated in R 3.4.1 by xtable 1.8-2 package
% Sun Sep 10 16:46:42 2017
\begin{table}[ht]
\centering
\begin{tabular}{rlrrrrrrrrrrr}
  \hline
 & SOV & Df2 & Sum.Sq2 & Mean.Sq2 & F.value2 & Pr..F.2 & Sum.Sq1 & Mean.Sq1 & NumDF1 & DenDF1 & F.value1 & Pr..F.1 \\ 
  \hline
1 & Days &   1 & 162702.65 & 162702.65 & 71.46 & 0.00 & 30031.01 & 30031.01 &   1 & 17.00 & 45.85 & 0.00 \\ 
  2 & Residuals & 178 & 405251.62 & 2276.69 &  &  &  &  &  &  &  &  \\ 
   \hline
\end{tabular}
\end{table}

1 个答案:

答案 0 :(得分:3)

查看svannoy的回答 Hiding NA's when printing a dataframe in knitr

```{r echo=FALSE, results='asis'}
options(knitr.kable.NA = '')
knitr::kable(lowerTri, digits=2)
```

(试图标记为可能的傻瓜,但我是一个SO菜鸟。)