R/clean.R

Defines functions clean

clean <- function(occurrences,
                  lon = "lon",
                  lat = "lat",
                  predictors,
                  clean_dupl = FALSE,
                  clean_nas = FALSE,
                  clean_uni = FALSE) {
# define occurrences to be lon and lat
  occurrences <- occurrences[, c(lon, lat)]
    if (exists("predictors")) {
        ori <- nrow(occurrences)
        if (clean_dupl == TRUE) {
            message("cleaning duplicates")
            dupls <- base::duplicated(occurrences)
            if (any(dupls)) {
            occurrences <- occurrences[!dupls, ]
            }
        }
        if (clean_nas == TRUE) {
            message("cleaning occurrences with no environmental data")
            presvals <- raster::extract(predictors, occurrences)
            compl <- complete.cases(presvals)
            if (all(compl == FALSE)) stop("All occurrence points are outside the predictor variable rasters")
            occurrences <- occurrences[compl, ]
        }
        if (clean_uni == TRUE) {
            if (clean_nas == FALSE) {
                warning("There may be points outside the raster")
                }
          message("cleaning occurrences within the same pixel")

            mask <- predictors[[1]]
            cell <- raster::cellFromXY(mask, occurrences)
            dup <- duplicated(cell)
            if (any(dup)) {
                occurrences <- occurrences[!dup, ]
                }
        }

        message(paste(ori - nrow(occurrences), "points removed"))
        message(paste(nrow(occurrences), "clean points"))
        return(occurrences)
    } else
        message("indicate the object with the predictive variables")
}
Model-R/modleR documentation built on Aug. 24, 2023, 6:50 p.m.