[GRASS-user] resampling question

Dylan Beaudette debeaudette at ucdavis.edu
Thu Oct 20 10:22:05 EDT 2011

On Thursday, October 20, 2011, Kirk Wythers wrote:
> I am curious how the r.resamp.stats function (when used to aggregate with 
the "method=mode" switch) handles an equal number of different cell scores? 
For example, lets say that aggregating 16 cells to 1cell yields 8 cells with a 
value of 4, and 8 cells with a value of 5. In this case what would 
r.resamp.stats report as the mode? And what method would it use to determine 
that value?

The neat thing about GRASS and other OSS, you can always take a peak at the 
source code. From my basic understanding of C, I don't think that there is 
anything here that would accommodate ties-- so the result of 
mode(4,4,4,4,5,5,5,5) would probably be 4 -- as it comes first. This is the 
behavior in R when using something like which.max(table(x)).

Here is what I found in: lib/stats/c_mode.c
void c_mode(DCELL * result, DCELL * values, int n, const void *closure)
    DCELL mode;
    int max;
    DCELL prev;
    int count;
    int i;

    n = sort_cell(values, n);

    max = 0;
    count = 0;

    for (i = 0; i < n; i++) {
	if (max == 0 || values[i] != prev) {
	    prev = values[i];
	    count = 0;


	if (count > max) {
	    max = count;
	    mode = prev;

    if (max == 0)
	Rast_set_d_null_value(result, 1);
	*result = mode;

Dylan E. Beaudette
USDA-NRCS Soil Scientist
California Soil Resource Lab

More information about the grass-user mailing list