# Get internal data
data("reefs")
# Reduce data for plotting
occdf <- reefs[1:250, ]
# Bin data using a hexagonal equal-area grid
ex1 <- bin_space(occdf = occdf, spacing = 500, plot = TRUE)
# Bin data using a hexagonal equal-area grid and sub-grid
ex2 <- bin_space(occdf = occdf, spacing = 1000, sub_grid = 250, plot = TRUE)
# EXAMPLE: rarefy
# Load data
occdf <- tetrapods[1:250, ]
# Assign to spatial bin
occdf <- bin_space(occdf = occdf, spacing = 1000, sub_grid = 250)
# Get unique bins
bins <- unique(occdf$cell_ID)
# n reps
n <- 10
# Rarefy data across sub-grid grid cells
# Returns a list with each element a bin with respective mean genus richness
df <- lapply(bins, function(x) {
# subset occdf for respective grid cell
tmp <- occdf[which(occdf$cell_ID == x), ]
# Which sub-grid cells are there within this bin?
sub_bin <- unique(tmp$cell_ID_sub)
# Sample 1 sub-grid cell n times
s <- sample(sub_bin, size = n, replace = TRUE)
# Count the number of unique genera within each sub_grid cell for each rep
counts <- sapply(s, function(i) {
# Number of unique genera within each sample
length(unique(tmp[which(tmp$cell_ID_sub == i), ]$genus))
})
# Mean richness across subsamples
mean(counts)
})
Run the code above in your browser using DataLab