Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think that what you are looking for is the <a href="http://stat.ethz.ch/R-manual/R-patched/library/base/html/sprintf.html" rel="nofollow"><strong>sprintf()</strong></a> function...<br> <code>sprintf</code> will save you from having the tests on n, to known how many leading zeros are needed.<br> Combined with the <a href="http://stat.ethz.ch/R-manual/R-patched/library/base/html/paste.html" rel="nofollow"><strong>paste()</strong> or <strong>paste0</strong></a> function, producing the desired file name becomes a one-liner.<br> Indeed it would be possibly to just use the sprintf() function alone, as in<br> <code>sprintf("intersections_track_calibrated_jma_from1951_%04d.csv", n)</code> but having a function to produce the file names and/or the "TrakID" may allow to hide all these file naming convention details away.</p> <p>Below, see sprintf() and paste0() in action, in the context of a convenience function created to produce the filename given a number n.</p> <pre><code>&gt; GetFileName &lt;- function(n) paste0("intersections_track_calibrated_jma_from1951_", sprintf("%04d", n), ".csv") &gt; GetFileName(1) [1] "intersections_track_calibrated_jma_from1951_0001.csv" &gt; GetFileName(13) [1] "intersections_track_calibrated_jma_from1951_0013.csv" &gt; GetFileName(321) [1] "intersections_track_calibrated_jma_from1951_0321.csv" &gt; </code></pre> <p>Of course, you could make the GetFileName function more versatile by adding parameters, some of which with a default value. In that fashion it could be used for both input and output file name (or any other file prefix/extension). For example:</p> <pre><code>GetFileName &lt;- function(n, prefix=NA, ext="csv") { if (is.na(prefix)) { prefix &lt;- "intersections_track_calibrated_jma_from1951_" } paste0(prefix, sprintf("%04d", n), ".", ext) } &gt; GetFileName(12) [1] "intersections_track_calibrated_jma_from1951_0012.csv" &gt; GetFileName(12, "output/tracks_crossing_regional_polygon_", "txt") [1] "output/tracks_crossing_regional_polygon_0012.txt" &gt; GetFileName(12, "output/tracks_crossing_regional_polygon_") [1] "output/tracks_crossing_regional_polygon_0012.csv" &gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload