Title: | A Collection of R Functions |
---|---|
Description: | A collection of R functions commonly used in FRB-CESAB projects. |
Authors: | Nicolas Casajus [aut, cre, cph] |
Maintainer: | Nicolas Casajus <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.0.0.9000 |
Built: | 2024-10-27 04:35:02 UTC |
Source: | https://github.com/FRBCesab/rutils |
Retrieves geographic coordinates (longitude and latitude) of any location in the World using the OpenStreetMap Data Search Engine Nominatim http://nominatim.openstreetmap.org.
address_to_coords(address)
address_to_coords(address)
address |
a |
A data.frame
with two columns: lon
, the longitude and lat
,
the latitude of the location.
address_to_coords("Houston, TX, USA") address_to_coords("5 rue de l'école de Médecine, Montpellier, France")
address_to_coords("Houston, TX, USA") address_to_coords("5 rue de l'école de Médecine, Montpellier, France")
Retrieves any location name in the World from geographic coordinates (longitude and latitude) using the OpenStreetMap Data Search Engine Nominatim http://nominatim.openstreetmap.org.
coords_to_address(coords)
coords_to_address(coords)
coords |
a |
A character
of length 1 with the OSM name of the location.
coords_to_address(c(43.61277, 3.873486)) coords_to_address(c(29.75894, -95.3677))
coords_to_address(c(43.61277, 3.873486)) coords_to_address(c(29.75894, -95.3677))
This function downloads a silhouette from the
PhyloPic database. With the new API, some
functions of the package
rphylopic
don't
work anymore. This function aims to temporary replace the function
rphylopic::image_data()
as this package seems to be looking for a new
maintainer.
get_phylopic_image(uuid, size = 512)
get_phylopic_image(uuid, size = 512)
uuid |
a |
size |
an |
An array of dimensions red x green x blue x alpha.
## Not run: mammals_pic <- get_phylopic_image("8cad2b22-30d3-4cbd-86a3-a6d2d004b201") ## Plot the silhouette ---- ggplot2::ggplot(x = Sepal.Length, y = Sepal.Width, data = iris, geom = "point") + rphylopic::add_phylopic(mammals_pic, alpha = 1) ## End(Not run)
## Not run: mammals_pic <- get_phylopic_image("8cad2b22-30d3-4cbd-86a3-a6d2d004b201") ## Plot the silhouette ---- ggplot2::ggplot(x = Sepal.Length, y = Sepal.Width, data = iris, geom = "point") + rphylopic::add_phylopic(mammals_pic, alpha = 1) ## End(Not run)
This function downloads a spatial vector of world countries boundaries as
defined by the IPBES (available at https://zenodo.org/record/3928281).
A zip
file is downloaded in the folder path
(working directory by
default) and files are extracted in the same folder. If this folder doesn't
exist it will be created.
Note: if you delete the zip file (after files extraction) and re-run this function, the zip
file will be downloaded again. It this is your wish
do not forget to use force = TRUE
to erase previous files.
Original Projection System: WGS84
get_world_basemap(path = ".", force = FALSE)
get_world_basemap(path = ".", force = FALSE)
path |
a |
force |
a |
No return value.
## Not run: ## Download and extract zip file ---- get_world_basemap() ## Check ---- list.files() ## Re-run function ---- get_world_basemap() # No download (zip file already present) get_world_basemap(force = TRUE) # Erase previous files ## End(Not run)
## Not run: ## Download and extract zip file ---- get_world_basemap() ## Check ---- list.files() ## Re-run function ---- get_world_basemap() # No download (zip file already present) get_world_basemap(force = TRUE) # Erase previous files ## End(Not run)
Estimates the hiking time for a planned route by considering horizontal distance, elevation gain and elevation loss.
This function implements the DIN 33466 of the German Alpine Club. Different speeds are assumed according to this standard:
4 kilometers per hour in the horizontal direction
300 vertical meters in the ascent per hour
500 vertical meters in the descent per hour
hiking_time(distance, elevation_gain, elevation_loss)
hiking_time(distance, elevation_gain, elevation_loss)
distance |
a |
elevation_gain |
a |
elevation_loss |
a |
A character
of length 1 returning the hiking time as "23h59"
.
## Lac du Montagnon par le Col d’Iseye ## https://www.visorando.com/randonnee-lac-du-montagnon-par-le-col-d-iseye/ hiking_time(distance = 20, elevation_gain = 1460, elevation_loss = 1460)
## Lac du Montagnon par le Col d’Iseye ## https://www.visorando.com/randonnee-lac-du-montagnon-par-le-col-d-iseye/ hiking_time(distance = 20, elevation_gain = 1460, elevation_loss = 1460)
Adds n leading zeros to a vector so that each element of the vector has the same number of characters. See examples below.
leading_zero(x)
leading_zero(x)
x |
a |
A character
vector of the same length as x
.
## On numeric vector ---- x <- 1:10 leading_zero(x) x <- 50:100 leading_zero(x) ## On floating vector ---- x <- c(1, 9.5, 10, 10.2) leading_zero(x) ## On character vector ---- x <- as.character(1:10) leading_zero(x) ## Creating sortable identifiers ---- x1 <- as.character(10:1) x2 <- leading_zero(x) sort(x1) sort(x2) x2 <- paste0("ID", x2) sort(x2)
## On numeric vector ---- x <- 1:10 leading_zero(x) x <- 50:100 leading_zero(x) ## On floating vector ---- x <- c(1, 9.5, 10, 10.2) leading_zero(x) ## On character vector ---- x <- as.character(1:10) leading_zero(x) ## Creating sortable identifiers ---- x1 <- as.character(10:1) x2 <- leading_zero(x) sort(x1) sort(x2) x2 <- paste0("ID", x2) sort(x2)
A shortcut for the function length
len(x)
len(x)
x |
an R object. |
See ?length for a complete description.
length(letters) len(letters)
length(letters) len(letters)
This function combines different matrices by row names and column names by
performing a 2-dimension full join. Gaps are filled with NA
(default) or
0
(argument na_to_zero
).
multi_merge(..., na_to_zero = FALSE)
multi_merge(..., na_to_zero = FALSE)
... |
one or several |
na_to_zero |
a |
A matrix
object.
mat1 <- matrix(rep(1, 9), nrow = 3) colnames(mat1) <- c("A", "B", "C") rownames(mat1) <- c("A", "B", "C") mat2 <- matrix(rep(1, 9), nrow = 3) colnames(mat2) <- c("D", "E", "F") rownames(mat2) <- c("D", "E", "F") mat3 <- matrix(rep(1, 9), nrow = 3) colnames(mat3) <- c("F", "G", "H") rownames(mat3) <- c("G", "A", "H") multi_merge(mat1, mat2, mat3) multi_merge(mat1, mat2, mat3, na_to_zero = TRUE)
mat1 <- matrix(rep(1, 9), nrow = 3) colnames(mat1) <- c("A", "B", "C") rownames(mat1) <- c("A", "B", "C") mat2 <- matrix(rep(1, 9), nrow = 3) colnames(mat2) <- c("D", "E", "F") rownames(mat2) <- c("D", "E", "F") mat3 <- matrix(rep(1, 9), nrow = 3) colnames(mat3) <- c("F", "G", "H") rownames(mat3) <- c("G", "A", "H") multi_merge(mat1, mat2, mat3) multi_merge(mat1, mat2, mat3, na_to_zero = TRUE)
Plot two variables using images png as points.Works with any king of png, the best stratgegy is to use png with no backgrounds. Parameter scale and size are important to tune to make the plot readable and not to heavy Must be plotted (or saved) on a 1:1 dimension
plot_2d_img( df, scale, size, pathimages, cexaxis, cexlab, labelx, labely, lm, xR, yR, colR, cexR, colline, lwline, ltyline, ... )
plot_2d_img( df, scale, size, pathimages, cexaxis, cexlab, labelx, labely, lm, xR, yR, colR, cexR, colline, lwline, ltyline, ... )
df |
a |
scale |
a numeric between 0 and 1 (used to resize the images) |
size |
the parameter size of the function imager::resize (between -0L and -100L) used to reduce the resolution of the image (proportion between 0 and 100%) |
pathimages |
path to the images |
cexaxis |
cex.axis of the plot function |
cexlab |
cex.lab of the plot function |
labelx |
label of the x axis |
labely |
label of the y axis |
lm |
TRUE or FALSE (compute and draw the lm) |
xR |
x coordinate of the position of the R2 on the plot |
yR |
y coordinate of the position of the R2 on the plot |
colR |
color of the R2 on the plot |
cexR |
size of the R2 on the plot |
colline |
col of the abline used to illustrate the lm |
lwline |
lwd of the abline used to illustrate the lm |
ltyline |
lty of the abline used to illustrate the lm |
... |
other plot options; see ?par (as mar or mpg) |
a plot
## Not run: set.seed(3) df <- cbind.data.frame(x=runif(10, 0, 10),y=runif(10, 0, 10), img_names=paste0("img_",rutils::leading_zero(1:10))) pathimages <- system.file("extdata", "plot2dimg", package = "rutils") plot_2d_img(df,scale=0.2,size=-35L,pathimages, lm=TRUE,xR=1.5,yR=10,colR="gray",cexR=1.5,colline="gray",lwline=2,ltyline=2, labelx="Variable x",labely="Variable y",mar=c(8, 9, 4.1, 2.1),mgp=c(6,2,0), cex.lab=2.5,cex.axis=1.5) ## End(Not run)
## Not run: set.seed(3) df <- cbind.data.frame(x=runif(10, 0, 10),y=runif(10, 0, 10), img_names=paste0("img_",rutils::leading_zero(1:10))) pathimages <- system.file("extdata", "plot2dimg", package = "rutils") plot_2d_img(df,scale=0.2,size=-35L,pathimages, lm=TRUE,xR=1.5,yR=10,colR="gray",cexR=1.5,colline="gray",lwline=2,ltyline=2, labelx="Variable x",labely="Variable y",mar=c(8, 9, 4.1, 2.1),mgp=c(6,2,0), cex.lab=2.5,cex.axis=1.5) ## End(Not run)
Renames any columns of a data.frame
(from 1 to as much columns as you
want).
rename_col(df, old.col.names, new.col.names)
rename_col(df, old.col.names, new.col.names)
df |
a |
old.col.names |
a |
new.col.names |
a |
The same data.frame
as df
.
df <- cbind.data.frame(a = 1:10, b = 1:10, c = 1:10, d = 1:10) df rename_col(df, old.col.names = c("a", "d"), new.col.names = c("aa", "dd"))
df <- cbind.data.frame(a = 1:10, b = 1:10, c = 1:10, d = 1:10) df rename_col(df, old.col.names = c("a", "d"), new.col.names = c("aa", "dd"))
Produce produces significance symbols for the values of p (ns., *, **, ***)
signi(p)
signi(p)
p |
a p value |
a character "ns", "", "", ""
signi(p=0.004)
signi(p=0.004)
Corrects species binomial name case.
to_binomial_name(x)
to_binomial_name(x)
x |
a |
A character
vector of same length as x
.
x <- c("sAlMO saLAR (Linnee, 1765)", "tyranosurus_REX rex.1") to_binomial_name(x)
x <- c("sAlMO saLAR (Linnee, 1765)", "tyranosurus_REX rex.1") to_binomial_name(x)