Not showing NAs in knitr::kable output as do xtable

1.4k views Asked by At

I want to omit NAs in the output of knitr::kable output same as the xtable do (See kable and xtable outputs below).

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

There are 1 answers

0
CCD On BEST ANSWER

Check out answer from svannoy on Hiding NA's when printing a dataframe in knitr

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

(Tried to mark as possible dupe, but I'm an SO noob.)