1

I'm working with a raster brick with 365 layers in R using the raster package. I want to transform all values in the raster brick for pixel index r,c from layer index start:stop. I've figured out how to extract these values from the brick:

year_mask[[start:stop]][r,c] 

...but when I try to assign a value to this subset of the raster brick, say by doing the following:

year_mask[[start:stop]][r,c] <- NA 

..then I get the following error:

Error in v[] <- value : incompatible types (from S4 to logical) in subassignment type fix

Any thoughts on how to assign a value or NA to a space-time index of a raster brick?

Emily
  • 825
  • 3
  • 10
  • 20

1 Answers1

0

This should do it:

year_mask[cellFromRowCol(year_mask,r,c)][start:stop] <- NA

Here, I'm assuming that r and c are scalars, and I'm using cellFromRowCol to get the cell index from the row and column indices.

If you have r and c as vectors of row and column indices and you want each row/col pair from those, see ?cellFromRowCol, then do the following:

cinds <- cellFromRowCol(year_mask,r,c)
year_mask[cinds][1:length(cinds), start:stop] <- NA

In both cases year_mask[cinds] returns a matrix with rows for each cell indexed and all layers as columns. The subsequent index indexes into this matrix.

If instead you want cells from all the combinations of r and c, then use cellFromRowColCombine to get the cinds.

Hope this helps.

aichao
  • 7,375
  • 3
  • 16
  • 18