Skip to content

Commit

Permalink
fst bearing to group centroid
Browse files Browse the repository at this point in the history
  • Loading branch information
robitalec committed Jul 19, 2024
1 parent f9fe323 commit 89536d1
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions R/bearing_to_group_centroid.R
Original file line number Diff line number Diff line change
@@ -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[])
}

0 comments on commit 89536d1

Please sign in to comment.