title: Summary and Histogram of large raster Layer
body: <p>I'm a very beginner in R and have a few questions regarding a large raster layer (Barrow_DEM.tif, 2,5GB) containing an elevation model. Additionaly there's a shape file (Barrow_DTLB.shp) with 10 polygons, for which I want to have the elevation data from the raster. Both files have the same spatial extend and the correct coordinates, projection, etc.</p>
  
  <pre><code>`setwd("...")
  Barrow_DEM &lt;- raster("Barrow_DEM.tif")
  Barrow_DTLB &lt;- st_read("Barrow_DTLB.shp")
  Barrow_DTLB_Buffer &lt;- st_read("Barrow_DTLB_BufferOUT.shp")
  Barrow_lake &lt;- crop(Barrow_DEM, extent(Barrow_DTLB))
  raster_lake &lt;- rasterize(Barrow_DTLB, Barrow_lake, mask = TRUE)
  summary(raster_lake)`
  </code></pre>
  
  <p>The result is shown as follows:</p>
  
  <pre><code>summary(raster_lake)
          layer
  Min.        0
  1st Qu.     2
  Median      3
  3rd Qu.     8
  Max.       12
  NA's        0
  Warning message:
  In .local(object, ...) :
    summary is an estimate based on a sample of 1e+05 cells (0.02% of all cells)
  </code></pre>
  
  <p>The result for the Histogram is the following:</p>
  
  <pre><code>hist(raster_lake, col=rgb(0,0,1,0.5),  breaks=100, ylab= "Frequency", xlab= "Height", main= "Histogram DTLB Barrow")
  Warning message: In .hist1(x, maxpixels = maxpixels, main = main, plot = plot, ...) :
    0% of the raster cells were used (of which 92% were NA). 7651 values used
  </code></pre>
  
  <p>I already tried this from another discussion here, but it didn't help me.
     <code>replaceNA &lt;- function(x, na.rm, ...){ 
    if(is.na(x[1]))
      return(0)
    else
      return(x)
  } 
  raster_lake &lt;- calc(raster_lake, fun = replaceNA)</code></p>
  
  <p>It seems like all data values in the new raster_lake file, which are outside the polygons got NA value. How do I get rid of these NAs? 
  I would like to get the full histogram as well as some statsitical data such as median, mean, min, max for all polygons and for each polygon seperately.</p>