Skip to content

Commit

Permalink
fst group centroid
Browse files Browse the repository at this point in the history
  • Loading branch information
robitalec committed Jul 19, 2024
1 parent fa88e1f commit 42f1ff9
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions R/group_centroid.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#' Group centroid
#'
#' Mean of individual locations in each group
#'
#' @param DT expecting group generated with eg. group_pts
#' @param coords character vector of column names for x, y
#' @param group group column default 'group'
#' @param na.rm if NAs should be removed in calculating mean location
group_centroid <- function(DT, coords, group = 'group', na.rm = FALSE) {
stopifnot(length(coords) == 2)

xcol <- first(coords)
ycol <- last(coords)

stopifnot(xcol %in% colnames(DT))
stopifnot(ycol %in% colnames(DT))
stopifnot(group %in% colnames(DT))

DT[, paste0('group_mean_', xcol) := mean(.SD[[xcol]], na.rm = na.rm), by = c(group)]
DT[, paste0('group_mean_', ycol) := mean(.SD[[ycol]], na.rm = na.rm), by = c(group)]
return(DT[])
}

0 comments on commit 42f1ff9

Please sign in to comment.