Geospatial Modelling Environment

r.sample (Sample Features)

Creates a random or stratified random sample of records in a feature table


This tool uses the R sample command to create a random or stratified random sample of records in a feature attribute table. Sampled records are coded with a 1 in a field in the attribute table, and all other records are 0. By default this field is named RNDSAMP, although you can specify any other field names using the ‘field’ parameter. This tool will not automatically overwrite an existing field unless you explicitly specify ‘overwrite=TRUE’ in the parameter list.

The number of features that are sampled can be specified as a count using the ‘size’ parameter, or as a proportion of the total number of records using the ‘proportion’ parameter. In the case of stratified sampling, the size parameter refers to the number of samples that will be selected within each stratum. By default, all strata are processed. If you wish to randomly sample among strata first, and then sample within strata then you must implement the stratalimit parameter (this controls the number of strata that are randomly selected).

For simple random samples a weight field can be specified with values that are proportional to the relative probability of selection. The values cannot be less than 0, but can be greater than 1 because the tool will automatically standardize the values by dividing my the maximum value (this ensures all the values are within the range 0-1). The weight field functionality is not yet implemented for the stratified sampling tool. If no weight field is specified, all records or features have an equal probability of being selected.

When using the size parameter, if you specify a sample size that is greater than the number of available records then a warning message will be generated, but all available features will be marked as being sampled.

This is a stochastic algorithm and is, therefore, very unlikely to yield the same results each time you run it. The ‘verbose’ option can be useful for checking that this algorithm is working correctly as it provides a detailed report of the sampling process. For further information on the R sample command, type ‘? sample’ at the R prompt, and press Enter.

This command is driven by R. Type ‘citation’ to see the suggested citation for R.


r.sample(in, size, proportion, [field], [weightfield], [stratified], [stratalimit], [overwrite], [verbose], [where]);

inthe input feature data source
sizethe number of features to sample (integer); takes precedence over ‘proportion’ if both are specified
proportionthe proportion of features to sample (0.0-1.0)
[field]the field that will record the selection (if it exists the program will stop, but see the overwrite option below) (default=RNDSAMP)
[weightfield]the field that contains relative probabilities of selection (does not apply to stratified samples; see documentation for details)
[stratified]the field that describes the strata in the data (typically an integer field representing unique group ID’s); the count or proportion options are applied at the level of the strata
[stratalimit]the number of strata to randomly sample (if undefined, all strata are processed)
[overwrite](TRUE/FALSE): if TRUE, if the output field already exists it will automatically be deleted and recreated, if FALSE the program stops with an error message if the field exists (default=FALSE)
[verbose](TRUE/FALSE): if TRUE, reports the sequence of sampled record numbers in the output window (default=FALSE)
[where]the selection statement that will be applied to the feature data source to identify a subset of features to process (see full Help documentation for further details)


r.sample(in=”C:dataplots.shp”, size=100, overwrite=TRUE);

r.sample(in=”C:dataplots.shp”, size=100, overwrite=TRUE, weightfield=”SELPROB”);

r.sample(in=”C:datalocs.shp”, field=”STRSEL”, proportion=0.1, stratified=”ANIMALID”, verbose=TRUE);


Please consider making a purchase to support the continued development of these tools  Read more...

Tips on how to use this interface efficiently 


Open Source GIS

Copyright © 2001-2014 Hawthorne L. Beyer, Ph.D., Spatial Ecology LLC    Connect on LinkedIn