#' Barycenter of a point cloud.
#'
#' This function calculates the barycenter (weighted centroid) of a point cloud.
#' @param cloud An m-by-n matrix of coordinates for m points in n-dimensional space.
#' @param weights An m-element vector of point masses (weights).
#' @export
barycenter <-
function(cloud, weights) {
# If w is missing...
if (missing(weights)) weights <- rep(1, nrow(cloud))
# If w is a scalar...
if (length(weights) == 1) weights <- rep(weights, nrow(cloud))
# Calculate barycenter
matrix(colSums(cloud * weights) / sum(weights), nrow = 1)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.