How to separate out bands from hyperspectral data using R

I'm having one hyperspctral data with 426 bands, with dim 250*250*426. I want to seperate each bands. I tried like this

img <- stack("name.tif")
rr <- array(, dim= c(250,250,426))
for(i in 1:426) {rr[,,i] <- raster(img, layer = i )}

but it's showing error as

number of items to replace is not a multiple of replacement length

I think the problem is, for dim(raster(img, layer = i )), I'm getting as 250*250*1 . How can I make it just as 250*250 ??

Edit : Or, Is it possible to take my hyperspectral image as a D x N matrix where D is the dimension of the data (no of HS bands = 426) and N is the number of HS pixels(250*250)

It is not clear at all what you mean with "I want to seperate each bands.". Apparently you want an array of the values. You can do

img <- brick("name.tif") # more efficient than stack
a <- as.array(img)

or, less efficiently:

rr <- array(, dim= c(250,250,426))
for(i in 1:426) {rr[,,i] <- values(raster(img, layer = i ))}

or

rr <- array(, dim= c(250,250,426))
for(i in 1:426) {rr[,,i] <- values(img[[i]])}
• Is it possible to take my hyperspectral image as a D x N matrix where D is the dimension of the data (no of HS bands) and N is the number of HS pixels; – bibinwilson Oct 7 '15 at 16:55
• v <- values(img) will give you N rows and D columns. For D x N do v <- t(v). – Robert Hijmans Oct 7 '15 at 20:46