Search code examples
rkablekableextra

How to remove NA's from a kableExtra kbl table?


When I run this code:

library(kableExtra)

table %>%
  kbl(caption ="Table 3: Data for Difference Equation Estimation.",col.names = c("Location",
                           "Year",
                           "Price",
                           "Per Capita Quantity",
                           "$\\Delta{P}$",
                           "$\\Delta{Q}$")) %>%
  kable_styling(full_width = F, position = "left")

I get the following:

enter image description here

How can I remove the NAs?


Solution

  • knitr::kable() is the foundation of kableExtra::kbl(). If you look at the help documentation for knitr::kable() it says:

    Missing values (NA) in the table are displayed as NA by default. If you want to display them with other characters, you can set the option knitr.kable.NA, e.g. options(knitr.kable.NA = '') to hide NA values.

    Therefore you might try:

    options(knitr.kable.NA = '')
    
    table %>%
      kbl(caption = "Table 3: Data for Difference Equation Estimation.",
          col.names = c("Location",
                        "Year",
                        "Price",
                        "Per Capita Quantity",
                        "$\\Delta{P}$",
                        "$\\Delta{Q}$")) %>%
      kable_styling(full_width = F, 
                    position = "left")