title: R : How to write an XYZ file from a SpatialPointsDataFrame?
body: <p>I have a SpatialPointsDataFrame which has one attribute (let's call it z for convenience) as well as lat/long coordinates.</p>
  
  <p>I want to write this out to an XYZ file (i.e. an ASCII file with three columns).</p>
  
  <p>Initially I tried</p>
  
  <pre><code>write.table(spdf, filename, row.names=FALSE)
  </code></pre>
  
  <p>but this wrote the z value first, followed by the coordinates, on each row. So it was ZXY format rather than XYZ. Not a big deal, perhaps, but annoying for other people who have to use the file.</p>
  
  <p>At present I am using what feels like a really horrible bodge to do this (given below), but my question is: is there a good and straightforward way to write a SPDF out as XYZ, with the columns in the right order? It seems as though it ought to be easy!</p>
  
  <p>Thanks for any advice.</p>
  
  <p>Bodge:</p>
  
  <pre><code>dfOutput &lt;- data.frame(x = coordinates(spdf)[,1], y = coordinates(spdf)[,2])
  dfOutput$z &lt;- data.frame(spdf)[,1]
  write.table(dfOutput, filename, row.names=FALSE)
  </code></pre>