title: How to apply sprintf function to r data frame to convert values to percentages while maintaining original data frame structure?
body: <p>I'm working through the options for formatting all numeric values in a data frame, and not just selected columns. I start with the following base data frame called &quot;c&quot; when running the code beneath it:</p>
  <pre><code>&gt; c
          A        B
  1 3.412324 2.234200
  2 3.245236 4.234234
      
  Related code:
  a &lt;- c(3.412324463,3.2452364)
  b &lt;- c(2.2342,4.234234)
  c &lt;- data.frame(A=a, B=b, stringsAsFactors = FALSE)
  </code></pre>
  <p>Next, I round all the numbers in the above &quot;c&quot; data frame to 2 decimal places, resulting in data frame &quot;d&quot; shown below with the related code immediately underneath:</p>
  <pre><code> &gt; d
           A    B
   [1,] 3.41 2.23
   [2,] 3.25 4.23
          
   Related code:
   d &lt;- as.data.frame(lapply(c, formatC, decimal.mark =&quot;.&quot;, format = &quot;f&quot;, digits = 2))
   d &lt;- sapply(d[,],as.numeric)
  </code></pre>
  <p>Last step, I'd like to express the above data frame &quot;d&quot; in percentages, in a new data frame called &quot;e&quot;. I get the below results as a list, using the code shown beneath it.</p>
  <pre><code>&gt; e
     X.341.0.. X.325.0.. X.223.0.. X.423.0..
  1    341.0%    325.0%    223.0%    423.0%
      
  Related code:
  e &lt;- as.data.frame(lapply(d*100, sprintf, fmt = &quot;%.1f%%&quot;))
  </code></pre>
  <p>How to I modify the code, in an efficient manner, to leave the data frame structure intact in deriving data frame &quot;e&quot;, the way it does when generating data frame &quot;d&quot;? It would be most helpful to see a solution in both base R and <code>dplyr</code>.</p>
  <p>I'm pretty sure the issue lies in my use of <code>lapply()</code> in creating data frame &quot;e&quot; (yes, by now I know <code>lapply()</code> spits out lists), but it worked fine in maintaining the data frame structure in creating data frame &quot;d&quot;!</p>
  <p>All values in the data frame are formatted the same, so there's no need to subset the columns etc.</p>