title: Export raster based on attribute table value in R
body: <p>I have a large raster file that has an associated raster attribute table (RAT):</p>
  
  <pre><code>class       : RasterLayer 
  dimensions  : 32550, 13900, 452445000  (nrow, ncol, ncell)
  resolution  : 1, 1  (x, y)
  extent      : 0, 13900, 0, 32550  (xmin, xmax, ymin, ymax)
  coord. ref. : NA 
  data source : H:\My Drive\raster.grd 
  names       : mr200_2012 
  values      : -318, 151078  (min, max)
  attributes  :
  
  ID COUNT  Value_of_interest 
  1   4        45
  2   7        1
  3   94       5
  4   5        7
  ...
  </code></pre>
  
  <p>The raster displays on the values, from -318 to 151078. I want to be able to export a raster with one of the attribute values as the 'value'. 
  For example changing the 'value' to the 'Value_of_interest' So that the raster displays a (min,max) of 1 to 45.</p>
  
  <p>This is very similar to how the <a href="http://desktop.arcgis.com/en/arcmap/latest/tools/spatial-analyst-toolbox/lookup.htm" rel="nofollow noreferrer">Lookup</a> tool works in ArcMap. I am just looking for a programmatic way to do this. </p>
  
  <p>I have not worked with RAT that much and am unfamiliar with some of the functionality of it. I have tried using a <code>data.table</code> and <code>data.frame</code> approach but the resultant table is really big and most times won't generate due to size. My thought was that I could create a <code>data.frame</code> with <code>xy</code> coordinates and then change the <code>z</code> value to the 'Value_of_interest'.</p>
  
  <p>I think this will be sufficient reproducible data:</p>
  
  <pre><code>xy &lt;- matrix(rnorm(200),20,20)
  xy&lt;-raster(xy)
  xy
  rat&lt;-data.frame("ID" = runif(10,0,10),
              "b" = runif(10,0,10),
              "variable of interest" = runif(10,10,20))
  
  
  levels(xy)[[1]]&lt;-rat
  </code></pre>