[R-sig-Geo] Substitute raster stack values based on matrix index

Thiago V. dos Santos thi_veloso at yahoo.com.br
Tue Apr 10 18:45:45 CEST 2018


I have a large (7000x7000, 10-layered) raster stack whose values range from 0 to 100.

I need to modify the raster values using the a "lookup table" consisted of a matrix which is 100 rows long by 10 cols wide, where the number of rows is associated with the 0-100 value range of the raster and the number of columns relates to the number of layers of the raster stack. 

The following code creates a toy example of my dataset:

# Create raster stackr <- raster(ncol=50, nrow=50)s <- stack(lapply(1:10, function(x) setValues(r, 1:ncell(r))))

# Create lookup tablem <- matrix(sample(100, 100*10, TRUE), 100, 10)

Therefore, I need to loop through each cell of the raster and check its value. For example, if the cell value is `20`, then the new value will be reassigned from the 20th line and 1st column in the matrix.

And so on for the 2nd raster stack layer and 2nd matrix column. And so on for the remaining raster stack layers and matrix columns.

Any ideas on how to do that?

I know this sounds a bit cumbersome, but that's how a few ISRIC-WISE's soil datasets are organized!
Greetings, -- Thiago V. dos Santos
Postdoctoral Research FellowDepartment of Climate and Space Science and EngineeringUniversity of Michigan

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list