[R] Sorting values within a raster
David Winsemius
dwinsemius at comcast.net
Fri Apr 22 04:36:18 CEST 2011
On Apr 21, 2011, at 9:33 PM, Sara Maxwell wrote:
> Hi David et al,
> Thanks for your help. I spent the afternoon and thought that would
> work but then I realized it was giving a different answer.
>
> I have counts ('hits') in each grid cell, and have then calculated
> the proportion of the total hits represented in each cell (such that
> the sum of all cells = 1). I want to take the cell with largest
> proportion, add the next largest proportion to it, etc until I reach
> 10% of the TOTAL number of 'hits'. Quantiles unfortunately are
> created using the total number of CELLS and not the total number of
> HITS, if that makes sense.
So sort, then cumsum and finally do findInterval. An example would
"focus the mind".
--
David.
>
> Any thoughts??
>
> Many many thanks,
> Sara
> _________________________________
>
> Sara M. Maxwell, Ph.D.
>
>
>
> On Apr 21, 2011, at 1:26 PM, David Winsemius wrote:
>
>>
>> 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
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list