Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's one way:</p> <pre><code>regmatches(tt, regexpr("[0-9].*[0-9]", tt)) </code></pre> <p>I assume that there are no other numbers in your file names. So, we just search for start of a number and use the greedy operator <code>.*</code> so that everything until the last number is captured. This is done using <code>regexpr</code> which'll get the position of matches. Then we use <code>regmatches</code> to extract the (sub)string out of these matched positions.</p> <hr> <p>where <code>tt</code> is:</p> <pre><code>tt &lt;- c("Species Count (2011-12-15-07-09-39).xls", "Species Count 0511.xls", "Species Count 151112.xls", "Species Count1011.xls", "Species Count2012-01.xls", "Species Count201207.xls", "Species Count2013-01-15.xls") </code></pre> <hr> <h2>Benchmarking:</h2> <h3>Note: Benchmarking results may differ between Windows and *nix machines (as @Hansi notes below under comments).</h3> <p>Quite some nice answers there. So, it's time for benchmarking :)</p> <pre><code>tt &lt;- rep(tt, 1e5) # tt is from above require(microbenchmark) require(stringr) aa &lt;- function() regmatches(tt, regexpr("[0-9].*[0-9]", tt)) bb &lt;- function() gsub("[A-z \\.\\(\\)]", "", tt) cc &lt;- function() str_extract(tt,'([0-9]|[0-9][-])+') microbenchmark(arun &lt;- aa(), agstudy &lt;- cc(), Jean &lt;- bb(), times=25) Unit: seconds expr min lq median uq max neval arun &lt;- aa() 1.951362 2.064055 2.198644 2.397724 3.236296 25 agstudy &lt;- cc() 2.489993 2.685285 2.991796 3.198133 3.762166 25 Jean &lt;- bb() 7.824638 8.026595 9.145490 9.788539 10.926665 25 identical(arun, agstudy) # TRUE identical(arun, Jean) # TRUE </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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