title: Calculate area for different land cover classes in a raster in R
body: <p>How can I get the the total area of my land cover classes 1,2,3,4,5 given the following example:</p>
  
  <pre><code>library("raster")
  r &lt;- raster(nrow=10, ncol=10)
  r&lt;-setValues(r,c(rep(1,20),rep(2,20),rep(3,20),rep(4,20),rep(5,20)))
  </code></pre>
  
  <p>One approach might be to subset the raster based on these values, however somethig like <code>a &lt;- area(r[getValues(r)==1])</code> did not work neither did <code>a &lt;- area(r[r==1])</code>. </p>
  
  <p>There is a solution provided on this <a href="http://caucasus-spiders.info/r-spatial/raster-basics-3/" rel="nofollow noreferrer">website</a>, however it requires creating new raster layers for each value to be analyzed. I would prefer rather not to do so since my original raster contains lots of different values and is very large. A similar approach is presented <a href="https://stackoverflow.com/questions/42425530/calculate-selected-area-from-stacked-raster-object?rq=1">here</a>, however it works only for small regions.</p>