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]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
Hi,
I think you want to reform your lookup table into a 2 column matrix with columns from_value and to_value, and then use raster::reclassify() The one thing that makes me hesitate is the lookup rule you describe. If a cell has a value of 20 then assign lut value m[20,1] So does that mean a cell value of 49 gets assigned m[40,10] ? What happens to a cell value of 3? These is no m[0,3]. Cheers, Ben > On Apr 10, 2018, at 12:45 PM, Thiago V. dos Santos via R-sig-Geo <[hidden email]> wrote: > > 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]] > > _______________________________________________ > R-sig-Geo mailing list > [hidden email] > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > Ben Tupper Bigelow Laboratory for Ocean Sciences 60 Bigelow Drive, P.O. Box 380 East Boothbay, Maine 04544 http://www.bigelow.org Tick Forecasting: https://eco.bigelow.org/ [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
In reply to this post by R-sig-geo mailing list
Maybe something along this line…..assuming I understood the problem properly
for (i in 1:nlayers(stackr)) { layer <- stackr[[i]] # get a layer from the stack ID_layer<- 1:ncell(layer[[1]]) # create cell ids for the layer for (j in 1:nrow(tablem)) { x <- rep(j,ncell(layer)) # LUT row number to test match replace <- layer[ID_Raster] == x # test if cell value matches LUT row number layer[ID_Raster[replace]] <- tablem[j,i] } stackr[[i]] <- layer # put the update layer back in the stack } --------------------------------------------------------------------- Katherine (Kay) Kilpatrick Oceanographer University of Miami Rosenstiel School for Marine and Atmospheric Science Satellite Remote Sensing Laboratory On Apr 10, 2018, at 12:45 PM, Thiago V. dos Santos via R-sig-Geo <[hidden email]<mailto:[hidden email]>> wrote: 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]] _______________________________________________ R-sig-Geo mailing list [hidden email]<mailto:[hidden email]> https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-geo&data=02%7C01%7Ckkilpatrick%40rsmas.miami.edu%7Cb434162221bf48ebef6408d59f028904%7C2a144b72f23942d48c0e6f0f17c48e33%7C0%7C1%7C636589755609404744&sdata=vjhu941iiiyOT%2BsRlvda7biPX6NAtr%2B07NcVvCGU8Ao%3D&reserved=0 [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
Hi Thiago -
Here is an approach using 'mapply' and 'reclassify'. I changed your example raster stack so the values range from 1:100 rather than 1:ncell(r) since that is what you stated in your problem: # Create raster stack r <- raster(ncol=50, nrow=50) s <- stack(lapply(1:10, function(x) setValues(r, rep(1:100, each=(ncell(r)/100))))) # Create lookup table m <- as.data.frame(matrix(sample(100, 100*10, TRUE), 100, 10)) # create new stack with lookup new.stack <- stack(mapply(FUN = function(x, y) { lookup <- matrix(cbind(1:length(y), y), ncol=2) reclassify(x, lookup) }, x = as.list(s), y = m)) plot(s); plot(new.stack) Cheers, Cotton -----Original Message----- From: R-sig-Geo [mailto:[hidden email]] On Behalf Of Kilpatrick, Katherine A Sent: Tuesday, April 10, 2018 11:44 AM To: Thiago V. dos Santos <[hidden email]> Cc: R-sig-geo Mailing List <[hidden email]> Subject: Re: [R-sig-Geo] Substitute raster stack values based on matrix index Maybe something along this line…..assuming I understood the problem properly for (i in 1:nlayers(stackr)) { layer <- stackr[[i]] # get a layer from the stack ID_layer<- 1:ncell(layer[[1]]) # create cell ids for the layer for (j in 1:nrow(tablem)) { x <- rep(j,ncell(layer)) # LUT row number to test match replace <- layer[ID_Raster] == x # test if cell value matches LUT row number layer[ID_Raster[replace]] <- tablem[j,i] } stackr[[i]] <- layer # put the update layer back in the stack } --------------------------------------------------------------------- Katherine (Kay) Kilpatrick Oceanographer University of Miami Rosenstiel School for Marine and Atmospheric Science Satellite Remote Sensing Laboratory On Apr 10, 2018, at 12:45 PM, Thiago V. dos Santos via R-sig-Geo <[hidden email]<mailto:[hidden email]>> wrote: 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]] _______________________________________________ R-sig-Geo mailing list [hidden email]<mailto:[hidden email]> https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-geo&data=02%7C01%7Ckkilpatrick%40rsmas.miami.edu%7Cb434162221bf48ebef6408d59f028904%7C2a144b72f23942d48c0e6f0f17c48e33%7C0%7C1%7C636589755609404744&sdata=vjhu941iiiyOT%2BsRlvda7biPX6NAtr%2B07NcVvCGU8Ao%3D&reserved=0 [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
Free forum by Nabble | Edit this page |