title: Is there a faster method to split string into substrings of a given length?
body: <p>I have some weather data with fixed width in columns, but length depends on variable (See below, data from GHCN, <a href="http://www1.ncdc.noaa.gov/pub/data/ghcn/daily/readme.txt" rel="nofollow noreferrer">http://www1.ncdc.noaa.gov/pub/data/ghcn/daily/readme.txt</a>). </p>
  
  <p>I would like to split them into a data.frame and have wrote some codes following the suggestion of @GSee (<a href="https://stackoverflow.com/questions/11619616/how-to-split-a-string-into-substrings-of-a-given-length/11619681#11619681">How to split a string into substrings of a given length?</a>). But it took about 4.3 s to process 6000 rows. </p>
  
  <p>Is there a faster method to process this dataset?</p>
  
  <p>Thanks for any suggestions. </p>
  
  <pre><code>temp &lt;- readLines(textConnection("NO000050550193801TMAX   53  I   51  I   10  I   22  I   56  I   31  I   30  I   24  I   38  I   25  I    2  I   32  I   75  I   71  I   98  I   96  I   57  I   55  I   54  I   60  I   91  I   75  I   94  I   82  I   89  I   46  I   26  I   68  I   62  I   46  I   37  I
  NO000050550193801TMIN   25  I   -6  I  -27  I    0  I    3  I  -14  I   -8  I   11  I   10  I  -11  I  -30  I  -23  I   22  I   38  I   47  I   33  I   13  I    5  I   10  I   29  I   42  I   45  I   51  I   44  I   35  I    5  I  -16  I  -20  I    5  I    2  I    5  I
  NO000050550193802TMAX   69  I   58  I   71  I   90  I   77  I   70  I   56  I   46  I   58  I   32  I   32  I   22  I   25  I   30  I   29  I   29  I   34  I   88  I   58  I   50  I   45  I   62  I   38  I   40  I   59  I  112  I   92  I   77  I-9999   -9999   -9999   
  NO000050550193802TMIN   11  I   26  I   16  I   35  I   44  I   21  I   19  I   22  I   20  I    6  I    6  I  -16  I  -22  I  -39  I  -28  I  -35  I  -33  I  -21  I  -13  I   15  I   26  I   17  I   -1  I    9  I   18  I   38  I   58  I   28  I-9999   -9999   -9999   
  NO000050550193803TMAX   81  I   84  I   89  I   86  I   86  I   74  I   54  I   74  I   83  I   64  I   75  I   77  I   66  I   91  I   82  I   84  I   89  I   84  I   94  I   85  I   82  I   89  I   74  I   84  I   81  I   58  I   72  I   58  I   86  I   84  I   89  I
  NO000050550193803TMIN   31  I   25  I   29  I   45  I   61  I   20  I    9  I    8  I   38  I   31  I    9  I   39  I   27  I   56  I   48  I   65  I   45  I   54  I   46  I   42  I   43  I   36  I   56  I   61  I   15  I   -2  I  -11  I   -2  I   12  I   30  I   24  I"))
  
  temp &lt;- rep(temp, 1000)
  system.time({
  
  out &lt;- strsplit(temp, '')
  out &lt;- as.matrix(do.call(rbind, out))
  pos_matrix &lt;- matrix(c(12, 16, 18, seq(0, 30) * 8 + 22,
      15, 17, 21, seq(0, 30) * 8 + 26), ncol = 2)
  out &lt;- apply(out, 1, function(x)
      {
          apply(pos_matrix, 1, function(y) 
              paste(x[y[1]:y[2]], collapse = ''))
      })
  })
  
  user  system elapsed 
  4.46    0.01    4.52 
  </code></pre>
  
  <p>EDIT WITH Ananda Mahto comment:</p>
  
  <pre><code>system.time({
  pos_matrix &lt;- matrix(c(12, 16, 18, seq(0, 30) * 8 + 22,
      15, 17, 21, seq(0, 30) * 8 + 26), ncol = 2)
  pos_matrix &lt;- lapply(seq(1, nrow(pos_matrix)), function(x) 
      {
          sprintf('substr(V1, %s, %s) f%s',
              pos_matrix[x,1], pos_matrix[x,2], x)
      })
  pos_matrix &lt;- paste(pos_matrix, collapse = ', ')
  out &lt;- data.frame(V1 = temp)
  
  out &lt;- sqldf(sprintf('select %s from out', pos_matrix))
  })
  
  user  system elapsed 
   0.4     0.0     0.4 
  </code></pre>
  
  <p>EDIT WITH jlhoward suggestion:</p>
  
  <pre><code>system.time({
  pos_matrix &lt;- matrix(c(12, 16, 18, seq(0, 30) * 8 + 22,
      15, 17, 21, seq(0, 30) * 8 + 26), ncol = 2)
  out &lt;- apply(pos_matrix, 1, function(x)
      {
          substr(temp, x[1], x[2])
      })
  })
  user  system elapsed 
  0.04    0.00    0.04
  </code></pre>