title: How to automatically convert many fields of a polygon shapefile to raster in R
body: <p>I have a shapefile representing Thiessen polygons. 
  <a href="https://i.stack.imgur.com/xdijL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xdijL.png" alt="enter image description hewith"></a></p>
  
  <p>Each polygon is associated with many values of a table. </p>
  
  <pre><code>thiessen &lt;- readOGR(dsn = getwd(), layer = poly)
  OGR data source with driver: ESRI Shapefile 
  Source: ".../raingauges/shp", layer: "thiessen_pol"
  with 10 features
  It has 5 fields
  head(thiessen)
    est est_name p001 p002 p003
  0   2   borges    1    8    2
  1   0     e018    2    4    3
  2   5  starosa    5   15    1
  3   6   delfim    4    2    2
  4   1     e087    1    1    3
  5   3     e010    0    1    0
  </code></pre>
  
  <p>The columns '<strong>est</strong>' and '<strong>est_name</strong>' are related to the ID and name of the rain gauges. The following columns are important to me and represent precipitation values on day 1, 2, and so on (in the exemple I kept just three days, but actually, I have 8 years of daily precipitation data).</p>
  
  <p>I need to convert the polygons to raster, but one raster for each field (column p001, p002, and so on) of the table.</p>
  
  <p>There is a simple way to convert polygons to raster with the function <a href="https://www.rdocumentation.org/packages/raster/versions/2.6-7/topics/rasterize" rel="nofollow noreferrer">rasterize</a> in R.</p>
  
  <pre><code>r_p001 &lt;- rasterize(thiessen, r, field = thiessen$p001)
  plot(r_p001)
  writeRaster(r_p001, filename=".../raingauges/shp/r_p001.tif")
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/1qO27.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1qO27.png" alt="enter image description here"></a></p>
  
  <p>The problem is that I need to set manually the field (column) of the table with the polygon values to be converted to raster. As I have about 2900 days (2900 columns with precipitation values for each rain gauge), it is impossible to do manually.</p>
  
  <p>The documentation does not help to clarify how to automate this process and I did not find anything on the internet to help me.</p>
  
  <p>Does anyone know how to automatically convert each field to raster and save as <em>tif</em> format?</p>