Skip to content

Instantly share code, notes, and snippets.

@sckott
sckott / CalcDists.R
Created April 20, 2011 14:18
get a matrix of pairwise geographic distances between locations
# Convert degrees to radians
deg2rad <- function(deg) return(deg*pi/180)
# Calculates the geodesic distance between two points specified by
# radian latitude/longitude using the Haversine formula
# Ouputs distance between sites 1 and 2 as meters
gcd.hf <- function(long1, lat1, long2, lat2) {
R <- 6371 # Earth mean radius [km]
delta.long <- (long2 - long1)
delta.lat <- (lat2 - lat1)
@sckott
sckott / clean_species.R
Last active October 5, 2021 14:19 — forked from ibartomeus/clean_species
Cleaning species taxonomy using taxize. I want to correct synonyms and typo's and drop incomplete cases.
# I have >1000 bees to check its name, so I want to automatize taxize for
# fixing misspellings when possible
# updating synonims to accepted names
# keeping ONLY accepted species (full resolved)
# As taxize has many functions I may be not being ptimal, commenets wellcomed.
# If you only want to use the function skip to the end,
# where I placed a wrap up function.
#example: good, synomin, typo, unexisting, genus only.
species <- c("Osmia rufa", "Osmia bicornis", "Osmia ruffa",
library(taxizedb)
## Registered S3 method overwritten by 'hoardr':
##   method           from
##   print.cache_info httr
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
## ------------------------------------------------------------------------
pop <- function(x, y) x[!y == names(x)]
## ------------------------------------------------------------------------
if (!requireNamespace("xml2", quietly = TRUE)) {
install.packages("xml2")
}
if (!requireNamespace("dplyr", quietly = TRUE)) {
install.packages("dplyr")
}
@sckott
sckott / loghistplot.R
Created January 10, 2012 13:41
Plot logistic regression with bar plots for distribution of 0 and 1 values on top and bottom x-axes.
# Define the function
loghistplot <- function(data) {
require(ggplot2); require(gridExtra) # load packages
names(data) <- c('x','y') # rename columns
# get min and max axis values
min_x <- min(data$x)
max_x <- max(data$x)
# process:
# - make a new branch: git checkout -b some-branch
# - run this script: Rscript path/to/package_strip_urls.R
# - update manual files and make sure package passes check
# - submit to cran
# - after accepted on cran - delete some-branch
have_pkg <- function(pkg) {
if (!requireNamespace(pkg, quietly = TRUE)) stop("need package ", pkg)
}
@sckott
sckott / slice.r
Last active December 18, 2020 10:16
Split up a vector into equal sized chunks e.g.: split c("a","b","d","e") into two vectors of two each (with roxygen doc). For some reason the function slice is missing from at least my R version (2.15.2 patched), so I wrote this to replace it.
#' Split up a vector into equal sized chunks
#'
#' @param input An input vector.
#' @param by The length of the resulting vectors.
#' @examples
#' vec <- c("a","b","d","e","f","g","h")
#' slice(vec, 3)
slice <- function(input, by=2){
starts <- seq(1,length(input),by)
tt <- lapply(starts, function(y) input[y:(y+(by-1))])
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.