R/distanceToTarget.R

Defines functions distanceToTarget

Documented in distanceToTarget

#' Calculates distances to a target point
#'
#' Given a dataset of points, it computes the Euclidean distances from each point to a specified target point. 
#' The function also identifies contiguous segments of points that fall within a specified radius around the target.
#'
#' @param data A matrix or data frame where each row represents a point in multi-dimensional space.
#' @param target A numeric vector representing the coordinates of the target point.
#' @param targetRadious A numeric value indicating the radius around the target point. 
#' Points within this radius are considered to be close to the target. Default is 0.
#' @return A list containing:
#' \item{distance}{A numeric vector of distances from each point in `data` to the `target`.}
#' \item{r}{A list with two components: `start`, indicating the starting indices of contiguous segments 
#' of points within the target radius, and `length`, indicating the lengths of these segments.}
#' \item{target_radious}{The radius around the target point.}
#' @examples
#' # Create an example data matrix
#' data <- matrix(c(1, 1, 2, 2, 3, 3), ncol = 2, byrow = TRUE)
#' target <- c(2, 2)
#' result <- distanceToTarget(data, target, targetRadious = 0)
#' print(result)
#' @export
distanceToTarget <- function(data, target, targetRadious = 0) {
  if (is.null(target)) {
    stop("The 'target' parameter is required but missing.")
  }
  distTarget <- function(v) {
    sqrt(sum((v - target)^2))
  }
  distance <- apply(data, 1, distTarget)
  r <- rle(distance <= targetRadious)
  start <- cumsum(c(1, r$lengths))[r$values == TRUE]
  length <- r$lengths[r$values == TRUE]
  names(start) <- NULL
  names(length) <- NULL
  r <- list(start = start, length = length)
  return(invisible(list(distance = distance, r = r, targetRadious = targetRadious)))
}

Try the BioTrajectory package in your browser

Any scripts or data that you put into this service are public.

BioTrajectory documentation built on June 8, 2025, 11:54 a.m.