title: format data.table column values (row wise) if numeric
body: <p>I am creating a summary <code>data.table</code> to be inserted in a <code>knitr</code> report using <code>xtable</code>. I would like to check each row value in each column if <code>is.numeric() == TRUE</code> and if it is, format the number, then revert it back to a character. If <code>is.numeric() == FALSE</code> then return the value. The actual <code>data.table</code> may have many columns.</p>
  
  <p>Here's what I have below, with the desired output at the bottom:</p>
  
  <pre><code>library(data.table)
  library(magrittr)
  
  dt &lt;- data.table(A = c("apples",
                         "bananas",
                         1000000.999),
                   B = c("red",
                         5000000.999,
                         0.99))
  
  dt
  
  a &lt;- dt[, lapply(.SD,
                   function(x)  {
                     if (is.na(is.numeric(x))) {
                       prettyNum(as.numeric(x), digits = 0, big.mark = ",")
                     } else {
                       x
                     }
                   })]
  
  a
  
  b &lt;- dt[, A := ifelse(is.na(is.numeric(A)),
                        format(as.numeric(A), digits = 0, big.mark = ","),
                        A)] %&gt;% 
    .[, B := ifelse(is.na(is.numeric(B)),
                    format(as.numeric(B), digits = 0, big.mark = ","),
                    B)]
  
  b
  b
  
  
  desired &lt;- data.table(A = c("apples",
                              "bananas",
                              "1,000,000"),
                        B = c("red",
                              "5,000,000",
                              "1"))
  
  desired
  </code></pre>
  
  <p>From my understanding <code>lapply</code> in the <code>j</code> argument of <code>data.table</code> syntax operates on the vector, so it can be used for functions like <code>mean()</code>, <code>sum()</code>, <code>na.approx()</code>, etc. and wouldn't necessarily work here. But I would like to loop over each column in the <code>data.table</code> without specifying each column name since there could be many columns and naming them would be cumbersome. It's kind of like I know the circle doesn't go in the square but I really want it to!</p>
  
  <p>I tried the <code>:= ifelse()</code> approach which I thought should work, but it seems to be returning the first element. On a different <code>data.table</code> where the column is entirely numeric, employing the same approach yields all <code>NA</code>. </p>
  
  <p>Thanks for any help!</p>