Sorting values within a raster
On Apr 21, 2011, at 3:23 PM, Sara Maxwell wrote:
I am working with a raster and want to take values assigned to each cell and sort them from largest to smallest, then cummulatively sum them together (in order from largest to smallest). I'll then be coding the individual cells such that the top 10% of the largest cell values can be visualize with one color, the next 10% with another and so on. I have tried a number of schemes but am having trouble figuring out how to chose the maximum value, code it and re-search the raster for the next highest value without replacement. I am assuming this requires a loop, unless there is a function that will do this automatically.
?quantile
Here is a sample dataset: library(raster) r <- raster(ncol=10, nrow=10) values(r) <- runif(ncell(r))
> quantile(values(r), prob=seq(0,1,by=0.1))
0% 10% 20% 30% 40%
0.004888148 0.106378528 0.217009097 0.307201289 0.364990984
50% 60% 70% 80% 90%
0.512523817 0.593382875 0.667916094 0.722919876 0.835839832
100%
0.996683575
You will also need findInterval()
If you want to create a factor that will assign your colors. perhaps
this could be used to index a suitable color vector:
fac <- findInterval(values(r), quantile(values(r),
prob=seq(0,1,by=0.1)) )
> fac
[1] 6 10 1 10 7 7 8 10 2 9 9 1 6 2 9 1 9 4 2 2
[21] 3 4 8 9 7 1 9 2 10 5 4 9 8 1 8 10 1 11 3 5
[41] 5 6 6 5 6 7 4 7 5 3 8 6 3 4 10 4 7 7 8 9
[61] 10 4 1 8 8 8 3 7 5 1 9 5 2 7 2 10 3 8 4 9
[81] 6 6 2 6 10 5 5 4 3 6 2 2 1 3 3 3 4 7 1 5
David Winsemius, MD
West Hartford, CT