Learn R Programming

SDMtune (version 1.1.0)

thinData: Thin Data

Description

Remove all but one location per raster cell. The function removes NAs and if more than one location falls within the same raster cell it selects randomly one.

Usage

thinData(coords, env, x = "x", y = "y")

Arguments

coords

data.frame or matrix with the coordinates, see details.

env

stack containing the environmental variables, or a single raster layer.

x

character. Name of the column containing the x coordinates, default is "x".

y

character. Name of the column containing the y coordinates, default is "y".

Value

a matrix or a data frame with the thinned locations.

Details

  • coords and env must have the same coordinate reference system.

  • The coords argument can contain several columns. This is useful if the user has information related to the coordinates that doesn't want to loose with the thinning procedure. The function expects to have the x coordinates in a column named "x", and the y coordinates in a column named "y". If this is not the case, the name of the columns containing the coordinates can be specified using the arguments x and y.

Examples

Run this code
# NOT RUN {
# Acquire environmental variables
files <- list.files(path = file.path(system.file(package = "dismo"), "ex"),
                    pattern = "grd", full.names = TRUE)
predictors <- raster::stack(files)

# Prepare background locations
bg_coords <- dismo::randomPoints(predictors, 9000)
nrow(bg_coords)

# Thin the locations
# There are probably few coordinates that have NAs for some predictors, the
# function will remove these coordinates. Note that the finction expects to
# the coordinates in two column named "x" and "y"
colnames(bg_coords)
thinned_bg <- thinData(bg_coords, env = predictors)
nrow(thinned_bg)

# Here we double the coordinates and run the function again
thinned_bg <- thinData(rbind(bg_coords, bg_coords), env = predictors)
nrow(thinned_bg)

# In case of a dataframe containing more than two columns (e.g. a dataframe
# with the coordinates plus an additional column with the age of the species)
# and custom column names, use the function in this way
age <- sample(c(1, 2), size = nrow(bg_coords), replace = TRUE)
data <- cbind(age, bg_coords)
colnames(data) <- c("age", "X", "Y")
thinned_bg <- thinData(data, env = predictors, x = "X", y = "Y")
head(data)
# }

Run the code above in your browser using DataLab