title: R kable/kableExtra, conditional formatting by percentage (with missing values)
body: <p>I am using kable and kableExtra and want to display a table with columns of percentages (in format &quot;95%&quot;). I would also like to do conditional formatting of the cell backgrounds based on the percent value (with multiple colors and cut points). In addition, there are a few NAs mixed in.</p>
  <p>I have tried some of the methods in <a href="https://stackoverflow.com/questions/55145441/conditional-formatting-with-kable-but-with-signs">this post</a>, but don't like the cell_spec background shading that does not shade the full cell, so would prefer to use column_spec within kable. However all the options I have found to display the percent convert the percent to a character variable, which then limits my ability to do the conditional formatting.</p>
  <p>As I was simplifying my example code to post here (after several hours of errors), I did finally get it working using startWith (which is an OK solution in this case because my cutpoints are at 80% and 90%, but not ideal for all cases).</p>
  <pre><code>df &lt;- tibble(
            x=c(1:6),
            percents =c (1, .95, .82, .77, .62, NA)
            )
      
  df$percents &lt;- percent(df$percents, accuracy = 1)
  
  kable(df, booktabs=TRUE, escape=FALSE ) %&gt;% 
     kable_styling() %&gt;% 
    column_spec(2, background = if_else(startsWith(df$percents, '100'), &quot;Darkgrey&quot;, 
                                if_else(startsWith(df$percents, '9'), &quot;Darkgrey&quot;, 
                                if_else(startsWith(df$percents, '8'), &quot;silver&quot;, &quot;lightgray&quot;, 
                                &quot;white&quot;), &quot;white&quot;), &quot;white&quot;))
  </code></pre>
  <p>I'm curious if anyone has another solution for this that preserves the ability to work with the percent values as numbers instead of characters.</p>
  <p>Thank You!</p>