I have different bands separated in different file directory.

I used Raster Calculator to calculate NDVI using "NIR1.tif"-"RED1.tif"/"NIR1.tif"+"RED1.tif" as formula.

The link below describes a loop for StackBand which I supposed is a file containing different bands. In my case, I have separate file path for NIR and RED.

Data is named NIR1...NIR2000.tif and RED1...RED2000.tif.

Using Loop for calculating NDVI in R?

I prefer using R but I am having difficulties in calling NIR and RED and putting them in a loop to calculate NDVI.

Below is my code:

NDVI< list.files("C:\\Users\\path\\Desktop\\PET\\sample\\", pattern="*.tif", all.files=F, full.names = FALSE, recursive = TRUE, ignore.case = FALSE) 
for (i in 1:length(NDVI)) 
NDVI <- raster(function(x,y) 
(x="NIR"+str(i)+".tif"), (y="red"+str(i)+".tif"), (x-y/x+y) 
writeRaster(NDVI,"NDVI", format="GTiff") 

I tried to make some revisions in the code.

  • Please edit that code out of the comment where it is unformatted into your question where it will look better. The code currently in the question is trying to compute using the filenames! – Spacedman Mar 20 '18 at 8:07
  • I already edited it. Still, it is not working. I am having difficulties in iterating the NIR and RED from 1 to 26 in the folder. – user2543 Mar 20 '18 at 9:23
  • You should also show us the error messages or describe what is happening, or what is not happening. – Spacedman Mar 20 '18 at 12:16
  • Error in .getGDALtransient(x, filename = filename, options = options, : filename exists; use overwrite=TRUE – user2543 Mar 21 '18 at 1:07

First, your formula is not right. You can't use (x-y/x+y)... Is the same than x - (y/x) + y. So correct this first.

I'll create some example data:


r <- raster()

userpath <- '/some/path/'

for (i in 1:2000) {
  temp <- setValues(r,sample(x = 0:1000, size = ncell(r), replace = T))
  writeRaster(temp, paste0(userpath,'NIR',i,'.tif'))
  temp <- setValues(r,sample(x = 0:1000, size = ncell(r), replace = T))
  writeRaster(temp, paste0(userpath,'RED',i,'.tif'))

Read NIR and RED in different objects:

NIR <- list.files(path = userpath, pattern = 'NIR*.*.tif',full.names = T)
RED <- list.files(path = userpath, pattern = 'RED*.*.tif', full.names = T)

Maybe, depend of rasters name, raster aren't in order when you use list.files(). In my case is something like:


You should extract the index from raster name to prevent undesired results:

i2 <- gsub(pattern = '.*NIR',replacement = '',x = NIR)
i2 <- gsub(pattern = '.tif$',replacement = '',x = i2)
i2 <- as.numeric(i2)

And finally, compute NDVI inside a loop and save rasters:

for (i in i2) {
  ndvitemp <- overlay(raster(NIR[i]),raster(RED[i]), fun = function(x,y) (x - y)/(x + y))
  writeRaster(ndvitemp, paste0(userpath,'NDVI',i,'.tif'))
  • + writeRaster(temp, paste0(userpath,'red',i,'.tif')) + } Error in .getGDALtransient(x, filename = filename, options = options, : filename exists; use overwrite=TRUE There is an error showing after the first writeRaster – user2543 Mar 21 '18 at 0:48
  • It’s because there is a file with the name that you want to use. Change the name or add overwrite = T – aldo_tapia Mar 21 '18 at 2:38
  • I added overwrite=T after writeRaster(temp, paste0(userpath,'RED',i,'.tif'), overwrite=T) } but it is still not working – user2543 Mar 21 '18 at 2:41
  • 1
    Hi, Mr. Aldo: I made some revisions in the code by removing Lines 4-9 and substituting for (i in 1:26) in for (i in seq_along((NIR)). It worked when I did manual calculation, the values are the same. Thank you very much and please continue extending your help to us beginners. – user2543 Mar 21 '18 at 3:49
  • 1
    I figure it out. I added zero padding before NDVI1.tif to make it NDVI0001.tif. – user2543 Mar 23 '18 at 5:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.