title: Read ascii grid data into matrix format
body: <p>I am trying to read a file in the ASCII TOMS grid format into R. I have been able to read it in such a way that it opens in R. However, iy opens as a linear matrix. A sumary of what the file contains is available here:</p>
  
  <pre><code>[Link](http://www.temis.nl/docs/README_TOMSASCII.pdf)
  </code></pre>
  
  <p>A sample of the data set could be downloaded here:</p>
  
  <pre><code>[Link](http://www.temis.nl/airpollution/no2col/no2monthscia.php?Year=2005&amp;Month=04)
  </code></pre>
  
  <p>The data set is for January 2006 and I just renamed it for easy access as there are quite a few I need to work with. I read it in using:</p>
  
  <pre><code>CCC&lt;-read.csv("no2_200601.asc",header=FALSE,skip=4,sep="\t")
  dim(CCC)
  [1] 52560    1
  </code></pre>
  
  <p>How do I read this into R so that data for each latitude is on a single row? I  feel this would help build a proper data structure.
  Note: Let me try and simply it ias I understand:<br>
   It means the structure is such that one line indicates the title e.g. lat=-89.9  and the next 144 lines with 20 elements each belong to row lat=-89.9; so my problem now is reading all these element before the next "lat=..." into one row.</p>
  
  <p>In addition, I just tried to loop it through a set of files using this:</p>
  
  <pre><code>NO2files&lt;-list.files(pattern=".asc", full.names=TRUE)
  f&lt;-lapply(NO2files, function (x) readLines (x))
  
  for (i in 1:length (NO2files)) {  
   function(x)
   i&lt;-readLines(x)
  pattern &lt;- "[[:digit:]]+(?=\\sbins)"
  m &lt;- regexpr(pattern, i[3], perl=TRUE)
  dim &lt;- regmatches(i[3], m)
  m &lt;- regexpr(pattern, i[4], perl=TRUE)
  dim[2] &lt;- regmatches(i[4], m)
  
  dim &lt;- as.integer(dim)
  
  pattern &lt;- "(?&lt;=undef=).*"
  m &lt;- regexpr(pattern, i[2], perl=TRUE)
  na_string &lt;- regmatches(i[2], m)
  
  dat1 &lt;- i[-(1:4)]
  sep &lt;- grepl("=", dat1, fixed=TRUE)
  dat2a &lt;- dat1[sep] 
  dat2b &lt;- dat1[!sep] 
  dat2b &lt;- lapply(dat2b, substring, 
              first=seq(1,nchar(dat2b[1]),4), 
              last=  seq(4,nchar(dat2b[1]),4))
  dat2b &lt;- unlist(dat2b)
  dat2b &lt;- as.numeric(dat2b)
  dat2b[dat2b==as.numeric(na_string)] &lt;- NA
  dat2b &lt;- matrix(dat2b, nrow=dim[2], byrow=TRUE)
  dat2b &lt;- dat2b[nrow(dat2b):1, ]
  }
  </code></pre>