-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgridRecords_mult
32 lines (24 loc) · 1.43 KB
/
gridRecords_mult
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# DEPRECATED; since fuzzySim v3.8, the original 'gridRecords' implements 'species' argument
# wrapper for 'fuzzySim::gridRecords' for multiple species at a time
# this function takes a raster stack and a set of spatial coordinates of some species' presence records,
# and returns a data frame with the presences and absences, as well as the corresponding values of the rasters
# in the grid of pixels (cells). All pixels without any presence point for each species are returned as absences.
gridRecords_mult <- function (rst, sp.data, sp.col, coord.cols, na.rm = TRUE)
{
# get a list of the presence coordinates for each species:
species <- as.character(unique(sp.data[ , sp.col]))
pres_coords_list <- lapply(species, function(x) sp.data[sp.data[ , sp.col] == x , coord.cols])
# grid records for each element of this list:
gridded_records_list <- lapply(pres_coords_list, function(x) fuzzySim::gridRecords(rst = rst, pres.coords = x))
names(gridded_records_list) <- species
# replace "presence" with the corresponding species in column names:
for (g in 1:length(gridded_records_list)) {
names(gridded_records_list[[g]])[1] <- as.character(species[g])
}
# separate extracted raster values from species presences:
extracted_vals <- gridded_records_list[[1]][ , -1]
for(spc in species) {
extracted_vals <- merge(gridded_records_list[[spc]][ , c(spc, "cells")], extracted_vals, by = "cells")
}
return(extracted_vals)
}