From 89536d18ed2724ee6b6dc1b046bf2a5bd45968b0 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 19 Jul 2024 12:07:12 -0300 Subject: [PATCH] fst bearing to group centroid --- R/bearing_to_group_centroid.R | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 R/bearing_to_group_centroid.R diff --git a/R/bearing_to_group_centroid.R b/R/bearing_to_group_centroid.R new file mode 100644 index 00000000..f9a35b25 --- /dev/null +++ b/R/bearing_to_group_centroid.R @@ -0,0 +1,28 @@ +#' Calculate absolute bearing to group centroid +#' +#' @param DT expects group_mean columns generated with group_centroid +#' @param coords character vector of column names for x, y +bearing_to_group_centroid <- function(DT, coords = NULL) { + pre <- 'group_mean_' + + stopifnot(length(coords) == 2) + + xcol <- first(coords) + ycol <- last(coords) + group_xcol <- paste0(pre, xcol) + group_ycol <- paste0(pre, ycol) + + stopifnot(xcol %in% colnames(DT)) + stopifnot(ycol %in% colnames(DT)) + stopifnot(group_xcol %in% colnames(DT)) + stopifnot(group_ycol %in% colnames(DT)) + + DT[, bearing_centroid := fifelse( + .SD[[xcol]] == .SD[[group_xcol]] & + .SD[[ycol]] == .SD[[group_ycol]], + NaN, + atan2(.SD[[group_ycol]] - .SD[[ycol]], + (.SD[[group_xcol]] - .SD[[xcol]])) + )] + return(DT[]) +}