在Knitr中打印数据框时隐藏NA [英] Hiding NA's when printing a dataframe in knitr
问题描述
我正在尝试使用xtable从数据框中打印knitr中的表格.下例中的表格尺寸为3x7,但第二列的第三行只有一个值.第三行中的其余单元格为"NA".
I'm trying to print a table in knitr from a data frame using xtable . The table in the example below has the dimensions 3x7 but the third row only has one value, in the second column. The rest of the cells in the third row are 'NA'.
当我编译文档时,是否有一种方法可以防止knitr在第三行中打印NA,所以我没有NA而是有空白?
When I compile the document, is there a way to prevent knitr from printing the NA's in the third row, so instead of NA I just have blank space?
感觉这应该是一个简单的解决方案,但我无法弄清楚隐藏NA的位置/方式.我需要对数据框进行更改吗?还是需要更改它的xtable或knitr选项?
It feels like this should be a simple solution but I can't work out where/how to hide the NA's. Is it a change I need to make to the data frame or is it an xtable or knitr option I need to change?
示例编织代码:
\documentclass{article}
<< data1, echo=FALSE, warning=FALSE, message=FALSE >>=
require(xtable)
FY.2014 <- 0.019
FY.2015 <- ((7000) - (6925.9)) / (6925.9)
FY.2016 <- ((8000) - (7000)) / (7000)
FY.2017 <- ((9000) - (8000)) / (8000)
FY.2018 <- ((10000) - (9000)) / (9000)
FY.2019 <- ((11000) - (10000)) / (10000)
PC <- data.frame(FY.2014, FY.2015, FY.2016, FY.2017, FY.2018, FY.2019)
PC.1 <- paste(round(PC*100, digits=1), "%", sep="")
FY.2014 <- 130.1
FY.2015 <- 7000 - 6925.9
FY.2016 <- 8000 - 7000
FY.2017 <- 9000 - 8000
FY.2018 <- 10000 - 9000
FY.2019 <- 11000 - 10000
AB <- data.frame(FY.2014, FY.2015, FY.2016, FY.2017, FY.2018, FY.2019)
AB.1 <- paste(round(AB , digits = 2))
FY.2014 <- as.numeric(c(""))
FY.2015 <- 7242.9
FY.2016 <- as.numeric(c(""))
FY.2017 <- as.numeric(c(""))
FY.2018 <- as.numeric(c(""))
FY.2019 <- as.numeric(c(""))
PF <- data.frame(FY.2014, FY.2015, FY.2016, FY.2017, FY.2018, FY.2019)
PF.1 <- paste(round(PF , digits = 2))
FTable <- rbind( PC.1, AB.1, PF.1)
rownames(FTable) <- c( 'Percent Change from the Previous Year',
'Absolute Change from Previous Year',
'December CY13 Forecast')
colnames(FTable) <- c( 'FY 2014', 'FY 2015', 'FY 2016', 'FY 2017', 'FY 2018', 'FY 2019')
@
\begin{document}
<<Table 1 , echo=FALSE, eval=TRUE, results='asis', fig.width = 5, fig.height = 2, message=FALSE, fig.align='center', warning=FALSE>>=
xFTable <- xtable(FTable, big.mark=",")
print(xFTable)
@
\end{document}
推荐答案
您可以将knitr选项knitr.kable.na =''设置为空格或所需的任何字符.
You can set the knitr option knitr.kable.na = '' '' for blanks, or whatever character you want.
```{r echo=FALSE, results='asis'}
options(knitr.kable.NA = '')
knitr::kable(lowerTri, digits=2)
```
这篇关于在Knitr中打印数据框时隐藏NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!