# R Code for Hyperspectral Indices

I am planning to use R to read hyperspectral images and calculate hyperspectral indices. There seems to be scripts available online, but mostly for multispectral images. For instance, this code for NDVI uses only two bands:

``````VI <- function(img, k, i) {
bk <- img[[k]]
bi <- img[[i]]
vi <- (bk - bi) / (bk + bi)
return(vi)
}

#knowing the band numbers for k(5) and i(3), the NDVI is:

ndvi <- VI(hyper_image, 5, 3)
``````

My dilemma is: I use a hyperspectral image with 100 bands. The above code becomes inefficient because I want to use all 100 bands in the formula.

``````#if, for example, these are my 100 bands: k, i, j, n, m, o, p, ....

#And just for the sake of illustration, this is a sample equation:

vi <- (bk + bi + bj + bn + bm + bo + bp + ....) / (bk + bi + bj + bn + bm + bo + bp + ....)

return(vi)

hyper_index <- VI(hyper_image, 5, 3, 6, 7, 8, 9, 1, ...)

#Obviously, doing this is excruciating!
``````

Can someone point me to existing R code for this procedure?

• If you want to use all 100 bands in some calculation then you write a loop. Its not excruciating. If your band indexes `VI(hyper_image, 5, 3, 6, 7, 8, 9, 1, ...)` are random then you're going to have to type them all out at some point in the right order. I don't see the problem... – Spacedman Oct 16 '19 at 16:17

## 1 Answer

You may consider using the R package hsdar as a starting point to carry out transformation of spectral response data as well calculation of vegetation indices. You will find additional information about this package in this article published in the Journal of Statistical Software. While the hyperSpec package might also be of use.

This tutorial provides useful information on working with hyperspectral data in R using the hdf5 format.