to_closest: Move occurrences to closest pixel with environmental data

View source: R/to_closest.R

to_closestR Documentation

Move occurrences to closest pixel with environmental data

Description

to_closest helps in changing the longitude and latitude values of occurrences with no environmental data, so they move to the closest pixel of a raster layer that contains relevant information. This process prevents NAs in future analyses.

Usage

to_closest(data, longitude, latitude, raster_layer, limit_distance)

Arguments

data

data.frame or matrix of occurrence records. Columns must include longitude and latitude. Other columns are optional and wont be changed.

longitude

(character) name of the column with longitude data.

latitude

(character) name of the column with latitude data.

raster_layer

RasterLayer to be used as a reference.

limit_distance

(numeric) maximun distance in km at which an occurrence could be to be moved. Records farther than this distance wont be moved.

Value

A data.frame with the corrected coordinates and four additional columns. The first of the new columns indicates the condition of the coordinates: Correct, if it was not moved because it was on a pixel with data; Moved, if it was moved to the nearest pixel; and Not_moved, if it was not moved because the occurrence was farther than the limit_distance to the closest pixel. The second new column indicates the distance to the closest pixel with data. The other two additional columns will contain the initial longitudes and latitudes.

Examples

data <- read.csv(system.file("extdata", "occurrences.csv",
                             package = "ellipsenm"))

var <- raster::raster(list.files(system.file("extdata", package = "ellipsenm"),
                                 pattern = "bio", full.names = TRUE)[1])

raster::plot(var)

out <- data.frame(as.character(data$species[1]), rbind(c(-103, 23), c(-99, 22.5),
                                                       c(-103.5, 25.6), c(-99, 27.3)))
colnames(out) <- colnames(data)

data <- rbind.data.frame(data, out)

points(data[, 2:3])

data1 <- to_closest(data, longitude = "longitude", latitude = "latitude",
                    raster_layer = var, limit_distance = 70)

points(data1[, 2:3], col = "red")

marlonecobos/ellipsenm documentation built on Oct. 18, 2023, 8:09 a.m.