#' distcalc
#'
#' Calculates pairwise geographic distance between locations on earth. Just a
#' convenient wrapper for fields::rdist.earth().
#'
#' @author John L. Darcy
#'
#' @param lat Numeric vector. Latitudes in decimal degree format.
#' @param lng Numeric vector. Longitudes in decimal degree format.
#' @param sampIDs Character vector. Sample identifiers. Only required
#' if output dist should have names associated.
#'
#' @return matrix containing all pairwise geographic distances in km.
#'
#' @examples
#' # library(specificity)
#' # attach(endophyte)
#' # geo_dists <- distcalc(metadata$Lat, metadata$Lon, metadata$SampleID)
#' # all(rownames(geo_dists) == metadata$SampleID)
#'
#' @export
distcalc <- function(lat, lng, sampIDs=NULL){
if(requireNamespace("fields", quietly=TRUE)){
longlats <- data.frame(lng, lat)
if(!is.null(sampIDs)){
rownames(longlats) <- sampIDs
}
distmat=fields::rdist.earth(as.matrix(longlats), miles=FALSE, R=NULL)
return((distmat))
}else{
stop("Package 'fields' is required for distcalc. Please install it and try again.")
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.