2

I have a raster r that contains values for each grid cell (which vary from one cell to another). I have other 24 rasters where the names of the rasters are:

the name of r1 is data-00.img, 
the name of r2 is data-01.img,
.........
the name of r24 is data-23.img

What I want to do is to look at the first pixel in r if NA,return NA but if there is value for example 12, search in the names of the other rasters (should be data-12.img) and then extract the corresponding value and put it instead of the 12 value in r.finally, do the same for all pixels.

 r <- raster(nrows=10, ncols=10); r <- setValues(r, 1:ncell(r))
 r1 <- raster(nrows=10, ncols=10);r1 <- setValues(r1, 1:ncell(r))
 r2 <- raster(nrows=10, ncols=10);r2 <- setValues(r1, 1:ncell(r))

My real data r could read as:

      cod <- file("C:\\Users\\data.img","rb")
      ne<- readBin(cod, numeric(), size=4,  n=1300*500, signed=TRUE)
      r <-raster(t(matrix((data= cet), ncol=500, nrow=1300)))

the other 24 rasters could be read as:

       dir2<- list.files("C:\\Users\\datasm", "*.img", full.names = TRUE)
       st <- stack(dir2)
  • It is easily do-able, yet few questions before: 1. Your first code line might contain a syntax error, since you do not define it as a "reading" function, i.e. raster(); and you don't apply " " to make it a string. 2. Where would you like the NA values to be applied to? back to the raster? a new raster/matrix? a list? an output vector? etc. Please clarify those. – dof1985 Apr 10 '15 at 9:25
  • It is my mistake. I was just showing their names but not assigning any thing to them.I edited my question. – usersam Apr 10 '15 at 12:35
  • The NA values should be applied back to the raster r. – usersam Apr 10 '15 at 12:42
1

Here is a code, a bit brute force, but your rasters do not seem to be that big. I give the code with some matrices as an example that you can run and see it works. Some modifications for your code are needed.

Basically, the trick here is to use grep() to find the number in the raster names' vector and to subset the name of th variable as a string. Then using the get function to use the string as a variable name and save it as a tmp variable. Last update the tmp raster and assign it back instead of the original one.

  ### Test data starts here
x<-matrix(c(1,2,NA,2),ncol=2,nrow=2)
x1<-x2<-matrix(0,nrow=2,ncol=2)

r<-x
RasterNames<-c("x","x1","x2") ###Note the this should be a character vector, 
                                ###populated by raster variable names
  ###This is the actual code
for (i in 1:nrow(r)) {
  for(j in 1:ncol(r)) {
    Value<-r[i,j]
    if (is.na(Value)) {
      r[i,j]<-Value
    } else {
      tmp<-get(RasterNames[grep(Value, RasterNames)])
      tmp[i,j]<-Value
      assign(RasterNames[grep(Value, RasterNames)],tmp)
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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