Title: | Collection of Useful R Utilities |
---|---|
Description: | While working on research projects, typical small functionalities are useful across these projects. Instead of copy-pasting these functions in all individual project repositories/folders, this package collects these functions for reuse by ourself and - if useful - others as well. |
Authors: | Hans Van Calster [aut, cre] (<https://orcid.org/0000-0001-8595-8426>, Research Institute for Nature and Forest (INBO)), Damiano Oldoni [aut] (<https://orcid.org/0000-0003-3445-7562>, Research Institute for Nature and Forest (INBO)), Stijn Van Hoey [aut] (<https://orcid.org/0000-0001-6413-3185>, Research Institute for Nature and Forest (INBO)), Dimitri Brosens [ctb] (<https://orcid.org/0000-0002-0846-9116>, Research Institute for Nature and Forest (INBO)), Peter Desmet [ctb] (<https://orcid.org/0000-0002-8442-8025>, Research Institute for Nature and Forest (INBO)), Frank Huysentruyt [ctb] (<https://orcid.org/0000-0002-3071-9126>, Research Institute for Nature and Forest (INBO)), Thierry Onkelinx [ctb] (<https://orcid.org/0000-0001-8804-4216>, Research Institute for Nature and Forest (INBO)), Floris Vanderhaeghe [ctb] (<https://orcid.org/0000-0002-6378-6229>, Research Institute for Nature and Forest (INBO)), Pieter Verschelde [ctb] (<https://orcid.org/0000-0002-9199-421X>, Research Institute for Nature and Forest (INBO)), Els De Bie [ctb] (<https://orcid.org/0000-0001-7679-743X>, Research Institute for Nature and Forest (INBO)), Nicolas Noé [ctb] (<https://orcid.org/0000-0002-9503-4750>, Research Institute for Nature and Forest (INBO)), Els Lommelen [ctb] (<https://orcid.org/0000-0002-3481-5684>, Research Institute for Nature and Forest (INBO)), Research Institute for Nature and Forest (INBO) [cph, fnd] |
Maintainer: | Hans Van Calster <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.4.0 |
Built: | 2024-12-15 04:17:11 UTC |
Source: | https://github.com/inbo/inborutils |
Convert encoding of character and factor variables in a dataframe
convertdf_enc(x, from = "", to = "UTF-8", sub = NA, colnames = FALSE)
convertdf_enc(x, from = "", to = "UTF-8", sub = NA, colnames = FALSE)
x |
An object with the |
from |
A character string describing the current encoding. |
to |
A character string describing the target encoding. |
sub |
character string. If not |
colnames |
Should column names be converted as well? |
Encoding strings: all R
platforms support ""
(for the
encoding of the current
locale), "latin1"
and "UTF-8"
.
See iconv
for more information.
The original object, with character variables (and levels of (character) factor variables) converted to the specified encoding.
Other Data_handling_utilities:
csv_to_sqlite()
,
df_factors_to_char()
data.frame
with coordinatesA dataset containing 52 coordinates as latitude and longitude
coordinate_example
coordinate_example
A data.frame
with 52 rows and 3 variables:
id
: resource identifier
latitude
: Latitude of the coordinates
longitude
: Longitude of the coordinates
Other datasets:
rain_knmi_2012
,
species_example
sqlite
databaseThe table can be a comma separated (csv
) or a tab separated (tsv
) or any
other delimited text file. The file is read in chunks. Each chunk is copied
in the same sqlite
table database before the next chunk is loaded into
memory.
See the INBO tutorial Handling large files in R
to learn more.
csv_to_sqlite( csv_file, sqlite_file, table_name, delim = ",", pre_process_size = 1000, chunk_size = 50000, show_progress_bar = TRUE, ... )
csv_to_sqlite( csv_file, sqlite_file, table_name, delim = ",", pre_process_size = 1000, chunk_size = 50000, show_progress_bar = TRUE, ... )
csv_file |
Name of the text file to convert. |
sqlite_file |
Name of the newly created |
table_name |
Name of the table to store the data table in the |
delim |
Text file delimiter (default ","). |
pre_process_size |
Number of lines to check the data types of the individual columns (default 1000). |
chunk_size |
Number of lines to read for each chunk (default 50000). |
show_progress_bar |
Show progress bar (default TRUE). |
... |
Further arguments to be passed to |
a SQLite
database
The callback
argument in the read_delim_chunked
function call
refers to the custom written callback function append_to_sqlite
applied
to each chunk.
Other Data_handling_utilities:
convertdf_enc()
,
df_factors_to_char()
## Not run: library(R.utils) library(dplyr) csv.name <- "2016-04-20-processed-logs-big-file-example.csv" db.name <- "2016-04-20-processed-logs-big-file-example.db" # download the CSV file example csv.url <- paste("https://s3-eu-west-1.amazonaws.com/lw-birdtracking-data/", csv.name, ".gz", sep = "" ) download.file(csv.url, destfile = paste0(csv.name, ".gz")) gunzip(paste0(csv.name, ".gz")) # Make a SQLite database sqlite_file <- "example2.sqlite" table_name <- "birdtracks" csv_to_sqlite( csv_file = csv.name, sqlite_file = sqlite_file, table_name = table_name ) # Get access to SQLite database my_db <- src_sqlite(sqlite_file, create = FALSE) bird_tracking <- tbl(my_db, "birdtracks") # Example query via dplyr results <- bird_tracking %>% filter(device_info_serial == 860) %>% select(date_time, latitude, longitude, altitude) %>% filter(date_time < "2014-07-01") %>% filter(date_time > "2014-03-01") %>% as_tibble() head(results) ## End(Not run)
## Not run: library(R.utils) library(dplyr) csv.name <- "2016-04-20-processed-logs-big-file-example.csv" db.name <- "2016-04-20-processed-logs-big-file-example.db" # download the CSV file example csv.url <- paste("https://s3-eu-west-1.amazonaws.com/lw-birdtracking-data/", csv.name, ".gz", sep = "" ) download.file(csv.url, destfile = paste0(csv.name, ".gz")) gunzip(paste0(csv.name, ".gz")) # Make a SQLite database sqlite_file <- "example2.sqlite" table_name <- "birdtracks" csv_to_sqlite( csv_file = csv.name, sqlite_file = sqlite_file, table_name = table_name ) # Get access to SQLite database my_db <- src_sqlite(sqlite_file, create = FALSE) bird_tracking <- tbl(my_db, "birdtracks") # Example query via dplyr results <- bird_tracking %>% filter(device_info_serial == 860) %>% select(date_time, latitude, longitude, altitude) %>% filter(date_time < "2014-07-01") %>% filter(date_time > "2014-03-01") %>% as_tibble() head(results) ## End(Not run)
The function checks the which columns are factors and converts these to character vectors
df_factors_to_char(rp)
df_factors_to_char(rp)
rp |
A dataframe |
all credits to Thierry Onkelinx
Other Data_handling_utilities:
convertdf_enc()
,
csv_to_sqlite()
df_factors_to_char(PlantGrowth) # column group will be chars as well
df_factors_to_char(PlantGrowth) # column group will be chars as well
More info, zie https://www.knmi.nl/kennis-en-datacentrum/achtergrond/data-ophalen-vanuit-een-script # nolint
download_knmi_data_hour( stations, variables, start_date, end_date, output_file = "knmi_download.txt" )
download_knmi_data_hour( stations, variables, start_date, end_date, output_file = "knmi_download.txt" )
stations |
list of integers. For an overview, see https://www.daggegevens.knmi.nl/klimatologie/uurgegevens |
variables |
list of variables, options are:
|
start_date |
date interpretable string to define start of the period |
end_date |
date interpretable string to define end of the period |
output_file |
path/filename of the output_file_name |
response containing, status_code, content,...
Other download_functions:
download_zenodo()
,
extract_soil_map_data()
,
read_kmi_data()
,
read_kml_file()
,
read_knmi_data()
,
read_mow_data()
## Not run: download_knmi_data_hour(c(310, 319), "PRCP", "2011-01-01", "2012-02-17", output_file = "knmi_output_download.txt" ) ## End(Not run)
## Not run: download_knmi_data_hour(c(310, 319), "PRCP", "2011-01-01", "2012-02-17", output_file = "knmi_output_download.txt" ) ## End(Not run)
This function will download an entire archive from Zenodo (https://zenodo.org). It only works for Zenodo created DOI (not when the DOI is for example derived from Zookeys.)
download_zenodo(doi, path = ".", parallel = TRUE, quiet = FALSE)
download_zenodo(doi, path = ".", parallel = TRUE, quiet = FALSE)
doi |
a doi pointer to the Zenodo archive starting with '10.5281/zenodo.'. See examples. |
path |
Path where the data must be downloaded. Defaults to the working directory. |
parallel |
Logical.
If |
quiet |
Logical ( |
Hans Van Calster, [email protected]
Floris Vanderhaeghe, [email protected]
Other download_functions:
download_knmi_data_hour()
,
extract_soil_map_data()
,
read_kmi_data()
,
read_kml_file()
,
read_knmi_data()
,
read_mow_data()
## Not run: # Example download of an archive containing a single zip download_zenodo(doi = "10.5281/zenodo.1283345") download_zenodo(doi = "10.5281/zenodo.1283345", quiet = TRUE) # Example download of an archive containing multiple files # using parallel download # (multiple files will be simultaneously downloaded) download_zenodo(doi = "10.5281/zenodo.1172801", parallel = TRUE) # Example download of an archive containing a single pdf file download_zenodo(doi = "10.5281/zenodo.168478") ## End(Not run)
## Not run: # Example download of an archive containing a single zip download_zenodo(doi = "10.5281/zenodo.1283345") download_zenodo(doi = "10.5281/zenodo.1283345", quiet = TRUE) # Example download of an archive containing multiple files # using parallel download # (multiple files will be simultaneously downloaded) download_zenodo(doi = "10.5281/zenodo.1172801", parallel = TRUE) # Example download of an archive containing a single pdf file download_zenodo(doi = "10.5281/zenodo.168478") ## End(Not run)
This function queries the the Flemish soil map # nolint attributes at a given coordinate, by using the affiliated WFS service provided by DOV. The user can pick the properties of interest. See the examples section to see how to obtain a full list of possible properties of interest. Coordinates should be given in the 'Belge 1972 / Belgian Lambert 72' (EPSG:31370) coordinate reference system. When outside the Flemish region, an NA value is given for each of the properties.
extract_soil_map_data(x_lam, y_lam, properties_of_interest)
extract_soil_map_data(x_lam, y_lam, properties_of_interest)
x_lam |
The numeric value of the X coordinate in CRS 'Belge 1972 / Belgian Lambert 72' (EPSG:31370). |
y_lam |
The numeric value of the Y coordinate in CRS 'Belge 1972 / Belgian Lambert 72' (EPSG:31370). |
properties_of_interest |
A vector or properties, as a subset of these
provided by the webservice (see the examples section for how to obtain the
list).
Default |
A data.frame
with the properties as column headers.
Other download_functions:
download_knmi_data_hour()
,
download_zenodo()
,
read_kmi_data()
,
read_kml_file()
,
read_knmi_data()
,
read_mow_data()
## Not run: library(inborutils) extract_soil_map_data(173995.67, 212093.44) # code to obtain list of all possible soil properties library(ows4R) library(purrr) wfs <- "https://www.dov.vlaanderen.be/geoserver/bodemkaart/bodemtypes/wfs" wfs_client <- WFSClient$new(wfs, serviceVersion = "1.1.0" ) wfs_client$ getCapabilities()$ findFeatureTypeByName("bodemkaart:bodemtypes")$ getDescription() %>% map_chr(function(x) { x$getName() }) extract_soil_map_data(173995.67, 212093.44, properties_of_interest = c( "Eenduidige_legende", "Textuurklasse" ) ) ## End(Not run)
## Not run: library(inborutils) extract_soil_map_data(173995.67, 212093.44) # code to obtain list of all possible soil properties library(ows4R) library(purrr) wfs <- "https://www.dov.vlaanderen.be/geoserver/bodemkaart/bodemtypes/wfs" wfs_client <- WFSClient$new(wfs, serviceVersion = "1.1.0" ) wfs_client$ getCapabilities()$ findFeatureTypeByName("bodemkaart:bodemtypes")$ getDescription() %>% map_chr(function(x) { x$getName() }) extract_soil_map_data(173995.67, 212093.44, properties_of_interest = c( "Eenduidige_legende", "Textuurklasse" ) ) ## End(Not run)
Development on gbif_species_name_match()
is complete, and for new code we
strongly recommend switching to rgbif::name_backbone_checklist()
, which is
easier to use, has more features, and still under active development.
This functions extends an existing dataframe with additional columns provided
by the GBIF taxonomic backbone and matched on the species (scientific) name,
which need to be an available column in the dataframe.
This function is essentially a wrapper around the existing rgbif
name_backbone
and extends the application to a data.frame. Such extension
has been added to rgbif via the function
rgbif::name_backbone_checklist. # nolint
For more information on the name matching API of GBIF on which rgbif relies,
see https://www.gbif.org/developer/species/#searching.
gbif_species_name_match( df, name = "name", gbif_terms = c("usageKey", "scientificName", "rank", "order", "matchType", "phylum", "kingdom", "genus", "class", "confidence", "synonym", "status", "family"), ... )
gbif_species_name_match( df, name = "name", gbif_terms = c("usageKey", "scientificName", "rank", "order", "matchType", "phylum", "kingdom", "genus", "class", "confidence", "synonym", "status", "family"), ... )
df |
|
name |
char column name of the column containing the names used for the name matching with the GBIF taxonomic backbone. Default: "name". |
gbif_terms |
list of valid GBIF terms to add as additional columns to
the data.frame. Default: |
... |
any parameter to pass to rgbif function |
a tibble data.frame with GBIF information as additional columns. If
none of the taxa in df
is matched, only the columns confidence
,
matchType
and synonym
are added. This behaviour is inherited by
rgbif::name_backbone
.
## Not run: library(readr) library(dplyr) species_list <- read_csv(paste0( "https://raw.githubusercontent.com/inbo", "/inbo-pyutils/master/gbif/gbif_name_match", "/sample.csv" ), trim_ws = TRUE, col_types = cols() ) # basic usage species_list %>% gbif_species_name_match() # pass optional parameters to name_backbone species_list %>% gbif_species_name_match(name = "name", kingdom = "kingdom", strict = TRUE) # select GBIF terms species_list %>% gbif_species_name_match(gbif_terms = c("scientificName", "rank")) ## End(Not run)
## Not run: library(readr) library(dplyr) species_list <- read_csv(paste0( "https://raw.githubusercontent.com/inbo", "/inbo-pyutils/master/gbif/gbif_name_match", "/sample.csv" ), trim_ws = TRUE, col_types = cols() ) # basic usage species_list %>% gbif_species_name_match() # pass optional parameters to name_backbone species_list %>% gbif_species_name_match(name = "name", kingdom = "kingdom", strict = TRUE) # select GBIF terms species_list %>% gbif_species_name_match(gbif_terms = c("scientificName", "rank")) ## End(Not run)
When retrieving a number of data-points, but the kind of CRS is not
provided/known (for any reason), this utility plots the data for different
CRS on a map to make comparison possible. The main function is
guess_crs
which creates the map with different options.
Custom projections can be provided by the user as well.
guess_crs( df, col_x, col_y, belgium = TRUE, crs_try = c("EPSG:4326", "EPSG:31370", "EPSG:28992", "EPSG:32631", "EPSG:3812", "EPSG:3035") )
guess_crs( df, col_x, col_y, belgium = TRUE, crs_try = c("EPSG:4326", "EPSG:31370", "EPSG:28992", "EPSG:32631", "EPSG:3812", "EPSG:3035") )
df |
data.frame with a x and y coordinate column |
col_x |
(character) name of the x (longitude) column |
col_y |
(character) name of the y (latitude) column |
belgium |
(logical) If |
crs_try |
(list)
|
For each of the given CRS, the coordinates columns are given the
the specified CRS In a next step, these CRS are converted to
wgs84
and plotted on a openstreetmap
background with leaflet.
The interactive map provides the possibility to select/unselect specific
layers.
crsuggest::guess_crs() for a (better) alternative
Other GIS_utilities:
plot_coordinates_on_map()
,
transform_coordinates()
## Not run: guess_crs(data, "x", "y") ## End(Not run)
## Not run: guess_crs(data, "x", "y") ## End(Not run)
Takes an integer (referring to number of bytes) and returns an optimally human-readable binary-prefixed byte size (KiB, MiB, GiB, TiB, PiB, EiB). The function is vectorised.
human_filesize(x)
human_filesize(x)
x |
A positive integer, i.e. the number of bytes (B). Can be a vector of file sizes. |
A character vector.
Floris Vanderhaeghe, [email protected]
human_filesize(7845691) v <- c(12345, 456987745621258) human_filesize(v)
human_filesize(7845691) v <- c(12345, 456987745621258) human_filesize(v)
These functions still work but will be removed (defunct) in the next version.
This function plots x/y coordinates from a data.frame on a (leaflet) map. The
coordinates are first converted to WGS84
in order to map them correctly on
the leaflet map (@seealso transform_coordinates()
). To do this, the
original Coordinate Reference System (CRS
) is asked.
plot_coordinates_on_map(df, col_x, col_y, projection, ...)
plot_coordinates_on_map(df, col_x, col_y, projection, ...)
df |
A data.frame with a x and y coordinate columns. |
col_x , col_y
|
Column names or positions of the x (longitude) and y
(latitude) column. They are passed to |
projection |
Projection string of class CRS-class ( |
... |
Additional arguments passed on to
|
Leaflet map with coordinates added as dots.
Other GIS_utilities:
guess_crs()
,
transform_coordinates()
## Not run: library(sf) data_pts <- data.frame( id = c(1, 2, 3), lat = c(51.23031, 50.76931, 50.21439), lon = c(5.083980, 3.829593, 3.289044), stringsAsFactors = FALSE ) # projection is of class CRS-class (sp) if (requireNamespace("sp")) { proj_crs_sp <- CRS("+init=epsg:4269") plot_coordinates_on_map(data_pts, "lon", "lat", proj_crs_sp) } # projection is of class crs-class (sf) proj_crs_sf <- st_crs(4269) plot_coordinates_on_map(data_pts, "lon", "lat", proj_crs_sf) # customize circles plot_coordinates_on_map(data_pts, "lon", "lat", proj_crs_sf, radius = 5, color = "red", stroke = FALSE, fillOpacity = 0.75 ) ## End(Not run)
## Not run: library(sf) data_pts <- data.frame( id = c(1, 2, 3), lat = c(51.23031, 50.76931, 50.21439), lon = c(5.083980, 3.829593, 3.289044), stringsAsFactors = FALSE ) # projection is of class CRS-class (sp) if (requireNamespace("sp")) { proj_crs_sp <- CRS("+init=epsg:4269") plot_coordinates_on_map(data_pts, "lon", "lat", proj_crs_sp) } # projection is of class crs-class (sf) proj_crs_sf <- st_crs(4269) plot_coordinates_on_map(data_pts, "lon", "lat", proj_crs_sf) # customize circles plot_coordinates_on_map(data_pts, "lon", "lat", proj_crs_sf, radius = 5, color = "red", stroke = FALSE, fillOpacity = 0.75 ) ## End(Not run)
When a label exceeds the maximum length as defined by the user, the function splits the text/label on the first space after the centre of the string. This is very useful for long labels in a plot
plot_label_splitter(label, maxlength)
plot_label_splitter(label, maxlength)
label |
A character array (the label) |
maxlength |
When the label exceeds this length, it will be split into a two-line label |
The function can be easily applied to a data.frame
by using e.g. sapply
plot_label_splitter("Exotic long label", 10)
plot_label_splitter("Exotic long label", 10)
data.frame
with KNMI
downloaded dataA dataset containing the rainfall from January 1st till February 1st for
Vlissingen
and Westdorpe
, as downloaded from KNMI
rain_knmi_2012
rain_knmi_2012
A data.frame
with 1536 rows and 9 variables:
value
: measured value
datetime
: datetime
of the measurement
unit
: unit (mm)
variable_name
: precipitation
longitude
: coordinate
latitude
: coordinate
location_name
: station name
source_filename
: filename from which the data was read
quality_code
: empty string as KNMI
does not provide this
Other datasets:
coordinate_example
,
species_example
KMI
data as data.frameLoad KMI
data as data.frame
read_kmi_data(filename, n_max = Inf)
read_kmi_data(filename, n_max = Inf)
filename |
path/filename of the |
n_max |
integer; shortcut for testing, loading just a section of the data |
data.frame
with the headers location_name
, datetime
(UTC),
value
, unit
, variable_name
, latitude
, longitude
and
source_filename
Other download_functions:
download_knmi_data_hour()
,
download_zenodo()
,
extract_soil_map_data()
,
read_kml_file()
,
read_knmi_data()
,
read_mow_data()
## Not run: # see vignettes for more examples file_path <- "kmi_file.txt" precipation_kmi <- read_kmi_data(file_path) ## End(Not run)
## Not run: # see vignettes for more examples file_path <- "kmi_file.txt" precipation_kmi <- read_kmi_data(file_path) ## End(Not run)
kml
fileExtract date and coordinate information from Google Maps kml
file
read_kml_file(filename)
read_kml_file(filename)
filename |
(char) filename/path of the |
data.frame
with columns datetime
, x
(numeric), y
(numeric)
Other download_functions:
download_knmi_data_hour()
,
download_zenodo()
,
extract_soil_map_data()
,
read_kmi_data()
,
read_knmi_data()
,
read_mow_data()
## Not run: df <- read_kml_file("BE1002.kml") ## End(Not run)
## Not run: df <- read_kml_file("BE1002.kml") ## End(Not run)
KNMI
datafile (precipitation) as data.frame
Remark that this function is specifically to extract the precipitation (RH)
data, coordinates are extracted from the header of the KNMI
data file
read_knmi_data(filename, n_max = Inf)
read_knmi_data(filename, n_max = Inf)
filename |
path/filename of the |
n_max |
integer; shortcut for testing, loading just a section of the data |
data.frame
with the headers location_name
, datetime
(UTC),
value
, unit
, variable_name
, latitude
, longitude
and
source_filename
Other download_functions:
download_knmi_data_hour()
,
download_zenodo()
,
extract_soil_map_data()
,
read_kmi_data()
,
read_kml_file()
,
read_mow_data()
## Not run: # see vignettes for more examples file_path <- "knmi_file.txt" knmi_data <- read_knmi_data(file_path) ## End(Not run)
## Not run: # see vignettes for more examples file_path <- "knmi_file.txt" knmi_data <- read_knmi_data(file_path) ## End(Not run)
Coordinate data is contained in the header of the file
read_mow_data(filename, n_max = Inf)
read_mow_data(filename, n_max = Inf)
filename |
path/filename of the |
n_max |
integer; shortcut for testing, loading just a section of the data |
data.frame
with the headers location_name
, datetime
(UTC),
value, unit
, variable_name
, latitude
, longitude
and source_filename
Other download_functions:
download_knmi_data_hour()
,
download_zenodo()
,
extract_soil_map_data()
,
read_kmi_data()
,
read_kml_file()
,
read_knmi_data()
## Not run: # see vignettes for more examples file_path <- "mow_file.txt" mow_data <- read_mow_data(file_path) ## End(Not run)
## Not run: # see vignettes for more examples file_path <- "mow_file.txt" mow_data <- read_mow_data(file_path) ## End(Not run)
This function will populate the content of /src
and /data
directories for a specific coding club session, the date of which is passed
as a parameter. Directories are created if needed. Files are downloaded
if not already present.
setup_codingclub_session( session_date = format(Sys.Date(), "%Y%m%d"), root_dir = ".", src_rel_path = "src", data_rel_path = "data" )
setup_codingclub_session( session_date = format(Sys.Date(), "%Y%m%d"), root_dir = ".", src_rel_path = "src", data_rel_path = "data" )
session_date |
The date of the coding-club session, in the |
root_dir |
Root directory where source and data subdirectories are
located. Default: |
src_rel_path |
Relative path for R script(s). Default: |
data_rel_path |
Relative path for data files. Default: |
## Not run: library(inborutils) # Download coding club files for session 2020-02-25 # R script(s) in `./src/20200225` and data files in `./data/20200225` setup_codingclub_session("20200225") # Specify the folders where you want to save R files and data files # R files will be downloaded in `./source` # Data files will be downloaded in `./dataframes` setup_codingclub_session( "20200225", src_rel_path = "source", data_rel_path = "dataframes" ) # You can modify the root directory even if it should be normally not needed. # For example you want to move the root to the parent directory, `../` setup_codingclub_session("20200225", root_dir = "../", src_rel_path = "source", data_rel_path = "dataframes" ) ## End(Not run)
## Not run: library(inborutils) # Download coding club files for session 2020-02-25 # R script(s) in `./src/20200225` and data files in `./data/20200225` setup_codingclub_session("20200225") # Specify the folders where you want to save R files and data files # R files will be downloaded in `./source` # Data files will be downloaded in `./dataframes` setup_codingclub_session( "20200225", src_rel_path = "source", data_rel_path = "dataframes" ) # You can modify the root directory even if it should be normally not needed. # For example you want to move the root to the parent directory, `../` setup_codingclub_session("20200225", root_dir = "../", src_rel_path = "source", data_rel_path = "dataframes" ) ## End(Not run)
data.frame
with species name columnA dataset containing 3 taxa to be matched with GBIF Taxonomy Backbone. The variable are as follows:
species_example
species_example
A data frame with 3 rows and 3 variables
speciesName
: name of the species
kingdom
: kingdom to which the species belongs
euConcernStatus
: level of concern according to EU directives
Other datasets:
coordinate_example
,
rain_knmi_2012
data.frame
columns.This function extracts x-y coordinates from a data.frame
by means of the
given coordinate reference system (CRS
), transforms them to the new CRS
and assign them back to the data.frame
columns.
transform_coordinates(df, col_x, col_y, crs_input, crs_output)
transform_coordinates(df, col_x, col_y, crs_input, crs_output)
df |
A |
col_x , col_y
|
Column names or positions of the x and y
column. They are passed to |
crs_input |
Projection string of class |
crs_output |
Projection string of class |
A data.frame
with the same columns, but transformed coordinates for
the x and y column values.
Other GIS_utilities:
guess_crs()
,
plot_coordinates_on_map()
library(sf) data_pts <- data.frame( id = c(1, 2), lat = c(51.23031, 50.76931), lon = c(5.083980, 3.829593), stringsAsFactors = FALSE ) # CRS-class (use sp package) if (requireNamespace("sp")) { sp_crs1 <- sp::CRS("+init=epsg:4269") sp_crs2 <- sp::CRS("+init=epsg:3857") transform_coordinates(data_pts, col_x = "lon", col_y = "lat", crs_input = sp_crs1, crs_output = sp_crs2 ) } # crs-class (use sf package) sf_crs1 <- st_crs(4269) sf_crs2 <- st_crs(3857) transform_coordinates(data_pts, col_x = "lon", col_y = "lat", crs_input = sf_crs1, crs_output = sf_crs2 ) if (requireNamespace("sp")) { # input projection is CRS-class (sp) and output projection crs-class (sf) transform_coordinates(data_pts, col_x = "lon", col_y = "lat", crs_input = sp_crs1, crs_output = sf_crs2 ) # input projection is crs-class (spf and output projection CRS-class (sp) transform_coordinates(data_pts, col_x = "lon", col_y = "lat", crs_input = sf_crs1, crs_output = sp_crs2 ) } # use names (character) of x-y columns transform_coordinates(data_pts, col_x = "lon", col_y = "lat", crs_input = sf_crs1, crs_output = sf_crs2 ) # use NSE of x-y columns transform_coordinates(data_pts, col_x = lon, col_y = lat, crs_input = sf_crs1, crs_output = sf_crs2 ) # use position of x-y columns transform_coordinates(data_pts, col_x = 3, col_y = 2, crs_input = sf_crs1, crs_output = sf_crs2 )
library(sf) data_pts <- data.frame( id = c(1, 2), lat = c(51.23031, 50.76931), lon = c(5.083980, 3.829593), stringsAsFactors = FALSE ) # CRS-class (use sp package) if (requireNamespace("sp")) { sp_crs1 <- sp::CRS("+init=epsg:4269") sp_crs2 <- sp::CRS("+init=epsg:3857") transform_coordinates(data_pts, col_x = "lon", col_y = "lat", crs_input = sp_crs1, crs_output = sp_crs2 ) } # crs-class (use sf package) sf_crs1 <- st_crs(4269) sf_crs2 <- st_crs(3857) transform_coordinates(data_pts, col_x = "lon", col_y = "lat", crs_input = sf_crs1, crs_output = sf_crs2 ) if (requireNamespace("sp")) { # input projection is CRS-class (sp) and output projection crs-class (sf) transform_coordinates(data_pts, col_x = "lon", col_y = "lat", crs_input = sp_crs1, crs_output = sf_crs2 ) # input projection is crs-class (spf and output projection CRS-class (sp) transform_coordinates(data_pts, col_x = "lon", col_y = "lat", crs_input = sf_crs1, crs_output = sp_crs2 ) } # use names (character) of x-y columns transform_coordinates(data_pts, col_x = "lon", col_y = "lat", crs_input = sf_crs1, crs_output = sf_crs2 ) # use NSE of x-y columns transform_coordinates(data_pts, col_x = lon, col_y = lat, crs_input = sf_crs1, crs_output = sf_crs2 ) # use position of x-y columns transform_coordinates(data_pts, col_x = 3, col_y = 2, crs_input = sf_crs1, crs_output = sf_crs2 )
Variance inflation factor
Variance inflation factor (lm
model)
Variance inflation factor (data.frame)
Variance inflation factor (default routine)
vif(mod, ...) ## S3 method for class 'lm' vif(mod, ...) ## S3 method for class 'data.frame' vif(mod, ...) ## Default S3 method: vif(mod, ...)
vif(mod, ...) ## S3 method for class 'lm' vif(mod, ...) ## S3 method for class 'data.frame' vif(mod, ...) ## Default S3 method: vif(mod, ...)
mod |
A model object (or data.frame). For the moment only |
... |
not used |
a matrix with for each variable de variance inflation factor, the degrees of freedom en the rescaled variance inflation factor based on the degrees of freedom.
{ vif(airquality) }
{ vif(airquality) }