Extract site x locations information from long format data
Source:R/fb_format_site_locations.R
fb_format_site_locations.Rd
Convert a flat data.frame
with site coordinates into a proper sf
object
that can then be used by other functions. This function assumes that
the coordinates are given in WGS84 (longitude vs. latitude). The function
automatically removes repeated coordinates from the input dataset.
Usage
fb_format_site_locations(
data,
site,
longitude,
latitude,
crs = sf::st_crs(4326),
na_rm = FALSE
)
Arguments
- data
a
data.frame
in a long format (see example).- site
a
character
of length 1. Name of the column with site labels.- longitude
a
character
of length 1. Name of the column with longitude. The function assumes coordinates are WGS84 (EPSG:4326).- latitude
a
character
of length 1. Name of the column with latitude. The function assumes coordinates are WGS84 (EPSG:4326).- crs
a
character
of length 1 or an object of classcrs
. Coordinate Reference System (CRS) of the specified coordinates. The CRS should be a valid CRS in R. It can either be a character like"+proj=longlat +datum=WGS84 +no_defs"
or as specified usingsf::st_crs()
likesf::st_crs(4326)
the default value.- na_rm
a logical value. If
TRUE
remove sites with incomplete coordinates. Default isFALSE
.
Examples
library("funbiogeo")
filename <- system.file("extdata", "raw_mammals_data.csv",
package = "funbiogeo")
all_data <- read.csv(filename)
head(all_data)
#> species order site longitude latitude count adult_body_mass
#> 1 sp_001 Cetartiodactyla fb_103 7.271821 59.09736 1 461900.8
#> 2 sp_001 Cetartiodactyla fb_1001 20.771821 52.59736 1 461900.8
#> 3 sp_001 Cetartiodactyla fb_102 6.771821 59.09736 1 461900.8
#> 4 sp_001 Cetartiodactyla fb_104 7.771821 59.09736 1 461900.8
#> 5 sp_001 Cetartiodactyla fb_101 6.271821 59.09736 1 461900.8
#> 6 sp_001 Cetartiodactyla fb_1000 20.271821 52.59736 1 461900.8
#> gestation_length litter_size max_longevity sexual_maturity_age diet_breadth
#> 1 235 1.25 324 668.2 1
#> 2 235 1.25 324 668.2 1
#> 3 235 1.25 324 668.2 1
#> 4 235 1.25 324 668.2 1
#> 5 235 1.25 324 668.2 1
#> 6 235 1.25 324 668.2 1
site_locations <- fb_format_site_locations(all_data, "site", "longitude",
"latitude")
head(site_locations)
#> Simple feature collection with 6 features and 1 field
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: 52.59736 ymin: 6.271821 xmax: 59.09736 ymax: 20.77182
#> Geodetic CRS: WGS 84
#> site geometry
#> 1 fb_103 POINT (59.09736 7.271821)
#> 2 fb_1001 POINT (52.59736 20.77182)
#> 3 fb_102 POINT (59.09736 6.771821)
#> 4 fb_104 POINT (59.09736 7.771821)
#> 5 fb_101 POINT (59.09736 6.271821)
#> 6 fb_1000 POINT (52.59736 20.27182)