-2

I am processing radiometry rasters. I wrote two bands in two separate files already:

setwd("D:/All_radio")
writeRaster(new,filename="NIR.envi",format="ENVI",overwrite=T)
writeRaster(new1,filename="SWIR.envi",format="ENVI",overwrite=T)

When I tried

ndii<-(("NIR.envi"- "SWIR.envi")/("NIR.envi"+ "SWIR.envi"))

the error occurs as "non-numeric argument to binary operator" How can I turn raster into numeric argument?

Yuyun He
  • 5
  • 1
  • 3

1 Answers1

5

You may need something like calc from the raster package.

 rast_stack <- stack(NIR.envi,SWIR.envi)
 fun <- function(x) { (x[1]-x[2])/(x[1]+x[2])}
 ndii <- calc(rast_stack, fun)

but there appear to be a few issues with your code anyway. In this line you are using strings rather than the rasters as variables.

(("NIR.envi"- "SWIR.envi")/("NIR.envi"+ "SWIR.envi"))

and you appear to be trying to create both rasters with the same data, in which case your output would always be 0. You are also creating a raster file but not creating an object in r.

user5219763
  • 1,284
  • 12
  • 19
  • Hi! Thank you for you reply! The second 'new' overwrites the first object so the two are not referring to the same data, I should specify it. However, I tried your code and it still says ' rast_stack' not found. I am not sure where the problem lies. Shall I import the two newly-written files again in R? I in a project and very new to R. Many thanks for your help! – Yuyun He May 04 '16 at 03:27
  • are you getting any other errors? You will need the raster data stored as a variable, so you could either read the files, or depending on the format of `new` and `new1`, you may be able to do something simple like `NIR.envi <- new`. Either way you need to have the rasters stored as a variable and then use the variable, rather than referring to the file as a string. – user5219763 May 04 '16 at 03:41