Hiding NA's when printing a dataframe in knitr

22,531

Solution 1

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

Solution 2

The trick I use is a bit brute-force, but it appears to work (in my use-cases, that is):

out <- knitr::kable(...)
cat(gsub('\\bNA\\b', '  ', out), sep='\n')
Share:
22,531

Related videos on Youtube

Matt
Author by

Matt

Updated on July 09, 2022

Comments

  • Matt
    Matt almost 2 years

    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'.

    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?

    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?

    Sample knitr code:

    \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}
    
    • Rich Scriven
      Rich Scriven over 9 years
      You could try print(xFTable, na.print = "") You may have to use print.default or a different print method to print the doc. But na.print appears in quite a few print methods
    • daroczig
      daroczig over 9 years
      Or if you are happy with markdown tables instead of LaTeX, then you could use pander where you can specify how to print NAs with the missing argument or global option.
    • Matt
      Matt over 9 years
      Thanks for the suggestion but I tried print(xFTable, na.print="") without any success. I didn't find na.print in the xtable documentation.
  • YvanR
    YvanR almost 8 years
    I guess '\\bNA\\n' should be '\\bNA\\b'?
  • Costin
    Costin over 7 years
    Where do you set it ? It doesn't work for me. Is it knitr.kable.na or knitr.kable.NA ? Do you use simple or double quotes ?
  • svannoy
    svannoy over 7 years
    @Costin, I've added context, I place it in a code chunk.
  • Matt
    Matt over 7 years
    You need to use knitr version 1.15 or above: github.com/yihui/knitr/releases/tag/v1.15.
  • r2evans
    r2evans about 7 years
    This answer should not longer be relevant given recent changes to knitr. I suggest using @svannoy's answer instead.
  • Will
    Will over 6 years
    In my case (Knit to pdf) it turned out to very useful, since the "newer" solution didn't work -- thanks!
  • Mikko
    Mikko almost 6 years
    This solution also works for papajas apa_table function (adding the comment to help internet search engines)
  • Ryan Hill
    Ryan Hill over 5 years
    @svannoy is it possible to give a formatted string e.g. 'null' but so it prints in italics?