I am writing a script to extract vegetation indices from hyperspectral datacubes. One of the indices is the Triangular Vegetation Index for which I wrote the following function:
TVI2 <- function(h,m,l)
{
(1.5*(2.5*(h-m)-1.3*(h-l))/sqrt((2*h+1)^2-(6*h-5*sqrt(m)-0.5)))
}
The variables stand for different wavelengths in the forumula. I have a rasterbrick object containing all 8 rasters and 101 bands per raster. To call a specific raster and wavelength I use:
raster(r[[1]], layer=1)
And this works just fine for calculating an index with just 2 variables, like the NDVI function:
ndvi1 <- NDVI(raster(r[[1]], layer=41),(raster(r[[1]], layer=71)))
However if I want to use this same set up for the TVI2 function:
tvi1 <- TVI2(raster(r[[1]], layer=71),(raster(r[[1]], layer=45),(raster(r[[1]], layer=21))))
I keep getting the following error:
Error: unexpected ',' in "tvi1 <- TVI2(raster(r[[1]], layer=71),(raster(r[[1]], layer=45),"
I cannot seem to figure out why. If I don' t use the raster call as the last variable, but just a number, the function runs fine.
Any ideas on how to solve this?