title: How can I create a raster mosaic using a raster-stack-list based on the max-values of one specific band?
body: <p>I'm trying to create a (nearly) cloadfree raster stack by comparing the NDVI values (cloads &lt; cloadfree vegetation) of my input rasters from every cell and selecting the "cell-stack" with the highest NDVI ("greenest" thus cloadfree) for the output. My area is about 2500x2500 cells.</p>
  
  <p>The code below is working but will take about 5-10 days for every year (and I'm comparing 20 years).</p>
  
  <p><em>snap</em> is my empty cropped output stack where values ar NA for not in observed area or 0 if they are in observed area.</p>
  
  <p><em>merged_raster.list</em> is a stack.list containing all of my cropped landsat scenes (Band 7 is the NDVI layer)</p>
  
  <pre><code>    for (year in dir(landsat_path)){
  
        for (snap_row in 1:nrow(snap)){
          for (snap_col in 1:ncol(snap)){
            snap_tmp &lt;- snap[[7]][snap_row, snap_col]
            if(!is.na(snap_tmp)){      #TRUE if current cell is in observed area
              best_scene &lt;- 0
              current_scene &lt;- 0
              for (scene in merged_raster.list) {  #comparing every szene to determine the best cell-value
                ndvi_tmp &lt;- scene[[7]][snap_row, snap_col]
                current_scene = current_scene + 1
                if (!is.na(ndvi_tmp)){
                  if (ndvi_tmp&gt;snap_tmp){
                    snap_tmp = ndvi_tmp
                    best_scene = current_scene 
                  }
                }
              }
              if(best_scene &gt; 0) {
                for (i in 1:7){
                snap[[i]][snap_row, snap_col] &lt;- merged_raster.list[[best_scene]][[i]][snap_row, snap_col]
               }
              }
            }
          }
        }
      }
  </code></pre>
  
  <p>Is there a way to increase the efficiency?</p>