I have several raster brick objects that I would like to combine into one raster with multiple field values for each cell, but I am not sure how to do this in R.

For example, let's say I have two layers in a 2*2 raster brick.

r <-raster(nrow=2,ncol=2)
r[] <- round(runif(ncell(r))* 10,0)
s <-raster(nrow=2,ncol=2)
s[] <- round(runif(ncell(r))* 10,0)
t = brick(r,s)

I would like to combine the values for r and s into a single raster with multiple values (at the object@data@attributes slot, I think) associated with each cell.

So, in the above example, if cell 1 = 1 in r and cell 1 = 2 in s, then the output that I desire is that the resulting raster contain two fields at @data@attributes, the first field = 1 and the second field = 2.

2 Answers 2


If you would like to keep the object type(s) as raster I would take a look at the ratify function although, I do not think that it is intended for numeric data.

r2 <- raster(nrow=10, ncol=10)
  r2[] = 1
    r2[51:100] = 2
      r2[3:6, 1:5] = 3

r2 <- ratify(r2)
  rat <- levels(r2)[[1]]
    rat$MyRATValue <- c(100,200,300)
      rat$code <- c(1,2,3)
        levels(r2) <- rat

You could also coerce the raster brick object to a SpatialPixelsDataFrame object where the @data slots holds attributes for each raster. This is however, not necessarily memory safe.

r <-raster(nrow=2,ncol=2)
  r[] <- round(runif(ncell(r))* 10,0)
  s <-raster(nrow=2,ncol=2)
    s[] <- round(runif(ncell(r))* 10,0)
      r = brick(r,s)

r <- as(r, "SpatialPixelsDataFrame")
  • Thanks for the reminder about using t as a variable name.
    – user44796
    Dec 16, 2014 at 20:12

I think that I have figured out a way to do it.

r <-raster(nrow=2,ncol=2)
r[] <- round(runif(ncell(r))* 10,0)
s <-raster(nrow=2,ncol=2)
s[] <- round(runif(ncell(r))* 10,0)
u <-raster(nrow=2,ncol=2)
u[] <- round(runif(ncell(r))* 10,0)
u = brick(u,r,s)
u@data@attributes[1][[1]] <- values(u)

At least in the toy example this does what I wanted it to do.

  • 1
    So, the question here is what are you going to do with this? Since raster objects operate in a different way the sp objects, I am not clear on the intention. You have just negated the memory safe aspects of a raster object and are storing the value attributes twice. With this solution, you may as well coerce to an sp object and operate directly on the pixels in a given function. Dec 16, 2014 at 21:01
  • BTW, you should check the validity of this solution. Since the placeholder class for this empty slot is list(), I would imagine that any functions intended for this slot is depending on this specific object class. Dec 16, 2014 at 21:07
  • I am basically trying to create a raster file similar to the National Land Cover Dataset (USGS) or NatureServe's natural areas raster file. These raster files have attribute tables associated with each cell. I agree that this may create issues, but can't come up with another way. That is the reason I posted the question.
    – user44796
    Dec 16, 2014 at 21:30
  • You are right, I was able to create it in the toy data set but it doesn't match what is in the National Landcover Dataset and the NatureServe natural community data.
    – user44796
    Dec 16, 2014 at 21:46
  • For NLCD the bit-depth creates a 0-255 range of values but only certain values are related to landcover classes. All yo need to do is to turn non-attributed values to NA. Since the numeric values are nationally consistent, you do not need the text attributes. I am not sure why you have multiple bands, unless the data is multi-year, it should be a single band not RGB. Dec 16, 2014 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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