From 39f921e75a18a4669ef4968c5240fb56e1f72279 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 19 Jul 2024 12:00:47 -0300 Subject: [PATCH 01/63] fst bearing to leader --- R/bearing_to_leader.R | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 R/bearing_to_leader.R diff --git a/R/bearing_to_leader.R b/R/bearing_to_leader.R new file mode 100644 index 00000000..ea86f71e --- /dev/null +++ b/R/bearing_to_leader.R @@ -0,0 +1,39 @@ +#' Calculate absolute bearing to group leader +#' +#' Leader as identified using group_leader by first position along +#' group axis of movement (return_rank = TRUE) +#' +#' Note: coords must be in order x, y +#' +#' @param DT +#' @param coords character vector columns names for x and y +#' @param group group column name generated by eg. group_pts +bearing_to_leader <- function(DT, coords = c('x', 'y'), group = 'group') { + stopifnot(first(coords) %in% colnames(DT)) + stopifnot(last(coords) %in% colnames(DT)) + stopifnot(group %in% colnames(DT)) + stopifnot('rank_dist_along_group_bearing' %in% colnames(DT)) + + check_has_leader <- DT[, .(has_leader = any(rank_dist_along_group_bearing == 1)), + by = c(group)][!(has_leader)] + + if (check_has_leader[, .N > 0]) { + warning('groups found missing leader (rank_dist_along_group_bearing == 1): \n', + check_has_leader[, paste(group, collapse = ', ')]) + } + + DT[, temp_leader_xcol := .SD[which(rank_dist_along_group_bearing == 1)], + .SDcols = first(coords), by = c(group)] + DT[, temp_leader_ycol := .SD[which(rank_dist_along_group_bearing == 1)], + .SDcols = last(coords), by = c(group)] + + DT[!group %in% check_has_leader$group, bearing_leader := fifelse( + .SD[[first(coords)]] == .SD[['temp_leader_xcol']] & + .SD[[last(coords)]] == .SD[['temp_leader_ycol']], + NaN, + atan2(.SD[['temp_leader_ycol']] - .SD[[last(coords)]], + (.SD[['temp_leader_xcol']] - .SD[[first(coords)]])) + )] + DT[, c('temp_leader_xcol', 'temp_leader_ycol') := NULL] + return(DT) +} From d77538d1d09dff5a6338538f98a9ee222a21d0f7 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 19 Jul 2024 12:00:52 -0300 Subject: [PATCH 02/63] fst distance to leader --- R/distance_to_leader.R | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 R/distance_to_leader.R diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R new file mode 100644 index 00000000..66cfb8ca --- /dev/null +++ b/R/distance_to_leader.R @@ -0,0 +1,35 @@ +#' Calculate distance to leader +#' +#' Leader as identified using group_leader by first position along +#' group axis of movement (return_rank = TRUE) +#' +#' @param DT +#' @param coords character vector of column names for x, y +#' @param group group column name generated by eg. group_pts +distance_to_leader <- function(DT, coords = c('x', 'y'), group = 'group') { + stopifnot(first(coords) %in% colnames(DT)) + stopifnot(last(coords) %in% colnames(DT)) + stopifnot(group %in% colnames(DT)) + stopifnot('rank_dist_along_group_bearing' %in% colnames(DT)) + + DT[, temp_N_by_group := .N, by = c(group)] + + check_has_leader <- DT[, .(has_leader = any(rank_dist_along_group_bearing == 1)), + by = c(group)][!(has_leader)] + + if (check_has_leader[, .N > 0]) { + warning('groups found missing leader (rank_dist_along_group_bearing == 1): \n', + check_has_leader[, paste(group, collapse = ', ')]) + } + + DT[!group %in% check_has_leader$group, + dist_leader := fifelse( + temp_N_by_group > 1, + as.matrix(dist(cbind(.SD[[1]], .SD[[2]])))[, which(.SD[[3]] == 1)], + 0 + ), + .SDcols = c(coords, 'rank_dist_along_group_bearing'), + by = c(group)] + DT[, temp_N_by_group := NULL] + return(DT) +} From a18029a3804b61fc73749f1a43100a0b22cfb315 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:20:41 -0300 Subject: [PATCH 03/63] fix clarity --- R/leader_direction_group.R | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/R/leader_direction_group.R b/R/leader_direction_group.R index 1bcb52b4..d5810840 100644 --- a/R/leader_direction_group.R +++ b/R/leader_direction_group.R @@ -30,10 +30,9 @@ #' #' @return \code{leader_direction_group} returns the input \code{DT} appended #' with a \code{position_group_direction} column indicating the position along -#' the group direction in the units of the projection and, optionally, a -#' \code{rank_position_group_direction} column indicating the -#' within group rank position along the group dirtection \code{return_rank = -#' TRUE}). +#' the group direction in the units of the projection and, optionally when +#' \code{return_rank = TRUE}, a \code{rank_position_group_direction} column +#' indicating the the ranked position along the group direction. #' #' A message is returned when \code{position_group_direction} or #' \code{rank_position_group_direction} columns already exist in the input From 4145732a72ece8b34dd2386deb8fd3ce90f17571 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:33:16 -0300 Subject: [PATCH 04/63] title --- R/distance_to_leader.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index 66cfb8ca..37bb9622 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -1,4 +1,4 @@ -#' Calculate distance to leader +#' Distance to group leader #' #' Leader as identified using group_leader by first position along #' group axis of movement (return_rank = TRUE) From 386276e298d8307664c0108c31e18f4968d19053 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:33:19 -0300 Subject: [PATCH 05/63] description --- R/distance_to_leader.R | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index 37bb9622..337705d8 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -1,7 +1,12 @@ #' Distance to group leader #' -#' Leader as identified using group_leader by first position along -#' group axis of movement (return_rank = TRUE) +#' \code{distance_to_leader} calculates the distance to the leader of each +#' spatiotemporal group. The function accepts a \code{data.table} with +#' relocation data appended with a \code{rank_position_group_direction} column +#' indicating the ranked position along the group direction generated with +#' \code{leader_direction_group(return_rank = TRUE)}. Relocation data should be +#' in planar coordinates provided in two columns representing the X and Y +#' coordinates. #' #' @param DT #' @param coords character vector of column names for x, y From ded82b093d2d09412d46e923001cbe7e9957f263 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:33:24 -0300 Subject: [PATCH 06/63] details --- R/distance_to_leader.R | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index 337705d8..328dd09a 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -8,9 +8,18 @@ #' in planar coordinates provided in two columns representing the X and Y #' coordinates. #' -#' @param DT -#' @param coords character vector of column names for x, y -#' @param group group column name generated by eg. group_pts +#' The \code{DT} must be a \code{data.table}. If your data is a +#' \code{data.frame}, you can convert it by reference using +#' \code{\link[data.table:setDT]{data.table::setDT}} or by reassigning using +#' \code{\link[data.table:data.table]{data.table::data.table}}. +#' +#' This function expects a \code{rank_dist_along_group_bearing} column +#' generated with \code{leader_direction_group(return_rank = TRUE)}, +#' a \code{group} column generated with the +#' \code{group_pts} function. The \code{coords} and \code{group} arguments +#' expect the names of columns in \code{DT} which correspond to the X and Y +#' coordinates and group columns. +#' distance_to_leader <- function(DT, coords = c('x', 'y'), group = 'group') { stopifnot(first(coords) %in% colnames(DT)) stopifnot(last(coords) %in% colnames(DT)) From 0fab804d1ae849f505352f3ae1e091f4b6ac9451 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:33:26 -0300 Subject: [PATCH 07/63] params --- R/distance_to_leader.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index 328dd09a..279f15d0 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -20,6 +20,7 @@ #' expect the names of columns in \code{DT} which correspond to the X and Y #' coordinates and group columns. #' +#' @inheritParams leader_direction_group distance_to_leader <- function(DT, coords = c('x', 'y'), group = 'group') { stopifnot(first(coords) %in% colnames(DT)) stopifnot(last(coords) %in% colnames(DT)) From 414b6513336e6b866543690db04af2235bc4643e Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:33:28 -0300 Subject: [PATCH 08/63] return --- R/distance_to_leader.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index 279f15d0..eb529c31 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -21,6 +21,12 @@ #' coordinates and group columns. #' #' @inheritParams leader_direction_group +#' +#' @return \code{distance_to_leader} returns the input \code{DT} appended with +#' a \code{distance_leader} column indicating the distance to the group leader. +#' +#' A message is returned when the \code{distance_leader} column is already exist in the input \code{DT} +#' because it will be overwritten. distance_to_leader <- function(DT, coords = c('x', 'y'), group = 'group') { stopifnot(first(coords) %in% colnames(DT)) stopifnot(last(coords) %in% colnames(DT)) From 9573104f61c5b66e528d2f974df91c8ee2cf6c48 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:33:36 -0300 Subject: [PATCH 09/63] export, family, seealso, references --- R/distance_to_leader.R | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index eb529c31..b7f01315 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -27,6 +27,16 @@ #' #' A message is returned when the \code{distance_leader} column is already exist in the input \code{DT} #' because it will be overwritten. +#' +#' @export +#' @family Distance functions +#' @seealso [leader_direction_group], [group_pts] +#' @references +#' +#' See examples of using distance to leader and position within group: +#' * +#' * +#' * distance_to_leader <- function(DT, coords = c('x', 'y'), group = 'group') { stopifnot(first(coords) %in% colnames(DT)) stopifnot(last(coords) %in% colnames(DT)) From 7546458f07e2bbd95b73d61e421c157ba03cadf4 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:33:44 -0300 Subject: [PATCH 10/63] examples --- R/distance_to_leader.R | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index b7f01315..ff389fe4 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -37,6 +37,44 @@ #' * #' * #' * +#' +#' @examples +#' # Load data.table +#' library(data.table) +#' \dontshow{data.table::setDTthreads(1)} +#' +#' # Read example data +#' DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc")) +#' +#' # Cast the character column to POSIXct +#' DT[, datetime := as.POSIXct(datetime, tz = 'UTC')] +#' +#' # Temporal grouping +#' group_times(DT, datetime = 'datetime', threshold = '20 minutes') +#' +#' # Spatial grouping with timegroup +#' group_pts(DT, threshold = 50, id = 'ID', +#' coords = c('X', 'Y'), timegroup = 'timegroup') +#' +#' # Calculate direction at each step +#' direction_step( +#' DT = DT, +#' id = 'ID', +#' coords = c('X', 'Y'), +#' projection = 32736 +#' ) +#' +#' # Calculate group centroid +#' centroid_group(DT, coords = c('X', 'Y')) +#' +#' # Calculate group direction +#' direction_group(DT) +#' +#' # Calculate leader in terms of position along group direction +#' leader_direction_group(DT, coords = c('X', 'Y')) +#' +#' # Calculate distance to leader +#' distance_to_leader(DT, coords = c('X', 'Y')) distance_to_leader <- function(DT, coords = c('x', 'y'), group = 'group') { stopifnot(first(coords) %in% colnames(DT)) stopifnot(last(coords) %in% colnames(DT)) From 1d0c7c4bdc87620d53d85d9463f4626955948e66 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:36:26 -0300 Subject: [PATCH 11/63] fix expected colname --- R/distance_to_leader.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index ff389fe4..e48024e4 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -13,7 +13,7 @@ #' \code{\link[data.table:setDT]{data.table::setDT}} or by reassigning using #' \code{\link[data.table:data.table]{data.table::data.table}}. #' -#' This function expects a \code{rank_dist_along_group_bearing} column +#' This function expects a \code{rank_position_group_direction} column #' generated with \code{leader_direction_group(return_rank = TRUE)}, #' a \code{group} column generated with the #' \code{group_pts} function. The \code{coords} and \code{group} arguments @@ -79,15 +79,15 @@ distance_to_leader <- function(DT, coords = c('x', 'y'), group = 'group') { stopifnot(first(coords) %in% colnames(DT)) stopifnot(last(coords) %in% colnames(DT)) stopifnot(group %in% colnames(DT)) - stopifnot('rank_dist_along_group_bearing' %in% colnames(DT)) + stopifnot('rank_position_group_direction' %in% colnames(DT)) DT[, temp_N_by_group := .N, by = c(group)] - check_has_leader <- DT[, .(has_leader = any(rank_dist_along_group_bearing == 1)), + check_has_leader <- DT[, .(has_leader = any(rank_position_group_direction == 1)), by = c(group)][!(has_leader)] if (check_has_leader[, .N > 0]) { - warning('groups found missing leader (rank_dist_along_group_bearing == 1): \n', + warning('groups found missing leader (rank_position_group_direction == 1): \n', check_has_leader[, paste(group, collapse = ', ')]) } @@ -97,7 +97,7 @@ distance_to_leader <- function(DT, coords = c('x', 'y'), group = 'group') { as.matrix(dist(cbind(.SD[[1]], .SD[[2]])))[, which(.SD[[3]] == 1)], 0 ), - .SDcols = c(coords, 'rank_dist_along_group_bearing'), + .SDcols = c(coords, 'rank_position_group_direction'), by = c(group)] DT[, temp_N_by_group := NULL] return(DT) From 3e1415d918dce1994ce67d15b89dd320a82ba955 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:36:31 -0300 Subject: [PATCH 12/63] fix arg defaults --- R/distance_to_leader.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index e48024e4..5eb2a741 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -75,7 +75,10 @@ #' #' # Calculate distance to leader #' distance_to_leader(DT, coords = c('X', 'Y')) -distance_to_leader <- function(DT, coords = c('x', 'y'), group = 'group') { +distance_to_leader <- function( + DT = NULL, + coords = NULL, + group = NULL) { stopifnot(first(coords) %in% colnames(DT)) stopifnot(last(coords) %in% colnames(DT)) stopifnot(group %in% colnames(DT)) From e995877c884794518c1fbed96d894cd496179135 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:52:26 -0300 Subject: [PATCH 13/63] nse --- R/distance_to_leader.R | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index 5eb2a741..c22a2159 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -79,10 +79,8 @@ distance_to_leader <- function( DT = NULL, coords = NULL, group = NULL) { - stopifnot(first(coords) %in% colnames(DT)) - stopifnot(last(coords) %in% colnames(DT)) - stopifnot(group %in% colnames(DT)) - stopifnot('rank_position_group_direction' %in% colnames(DT)) + # Due to NSE notes + # distance_leader <- zzz_N_by_group <- NULL DT[, temp_N_by_group := .N, by = c(group)] From b80adb231a37432d8793a2fb074367c6d7844137 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:52:36 -0300 Subject: [PATCH 14/63] checks --- R/distance_to_leader.R | 51 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index c22a2159..b73d3aa3 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -82,10 +82,55 @@ distance_to_leader <- function( # Due to NSE notes # distance_leader <- zzz_N_by_group <- NULL - DT[, temp_N_by_group := .N, by = c(group)] + if (is.null(DT)) { + stop('input DT required') + } + + if (length(coords) != 2) { + stop('coords requires a vector of column names for coordinates X and Y') + } + + if (is.null(group)) { + stop('group column name required') + } + + if (!group %in% colnames(DT)) { + stop('group column not present in input DT, did you run group_pts?') + } + + check_cols <- c(coords, group) - check_has_leader <- DT[, .(has_leader = any(rank_position_group_direction == 1)), - by = c(group)][!(has_leader)] + if (any(!(check_cols %in% colnames(DT)))) { + stop(paste0( + as.character(paste(setdiff( + check_cols, + colnames(DT) + ), collapse = ', ')), + ' field(s) provided are not present in input DT' + )) + } + + if (any(!(DT[, vapply(.SD, is.numeric, TRUE), .SDcols = coords]))) { + stop('coords must be numeric') + } + + # check exists, is numeric + leader_col <- 'rank_position_group_direction' + + if (!leader_col %in% colnames(DT)) { + stop(paste0( + leader_col, + ' column not present in input DT, ', + 'did you run leader_direction_group(return_rank = TRUE)?')) + } + + out_col <- 'distance_leader' + if (out_col %in% colnames(DT)) { + message( + paste0(out_col, ' column will be overwritten by this function') + ) + data.table::set(DT, j = out_col, value = NULL) + } if (check_has_leader[, .N > 0]) { warning('groups found missing leader (rank_position_group_direction == 1): \n', From c30f48a0b6fe29e3e58470f533847029586dec27 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:52:52 -0300 Subject: [PATCH 15/63] fix long lines --- R/distance_to_leader.R | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index b73d3aa3..d507adb0 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -132,19 +132,29 @@ distance_to_leader <- function( data.table::set(DT, j = out_col, value = NULL) } + + DT[, zzz_N_by_group := .N, by = c(group)] + + check_has_leader <- DT[, .( + has_leader = any(rank_position_group_direction == 1)), + by = c(group)][!(has_leader)] + if (check_has_leader[, .N > 0]) { - warning('groups found missing leader (rank_position_group_direction == 1): \n', - check_has_leader[, paste(group, collapse = ', ')]) + warning( + 'groups found missing leader (rank_position_group_direction == 1): \n', + check_has_leader[, paste(group, collapse = ', ')] + ) } DT[!group %in% check_has_leader$group, - dist_leader := fifelse( - temp_N_by_group > 1, - as.matrix(dist(cbind(.SD[[1]], .SD[[2]])))[, which(.SD[[3]] == 1)], + c(out_col) := fifelse( + zzz_N_by_group > 1, + as.matrix( + stats::dist(cbind(.SD[[1]], .SD[[2]])) + )[, which(.SD[[3]] == 1)], 0 ), .SDcols = c(coords, 'rank_position_group_direction'), by = c(group)] - DT[, temp_N_by_group := NULL] - return(DT) + } From 032c97c45a4c4fbc7f2a80b4974d9c8883bcce10 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:52:56 -0300 Subject: [PATCH 16/63] use data.table::set --- R/distance_to_leader.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index d507adb0..e3d13447 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -157,4 +157,7 @@ distance_to_leader <- function( .SDcols = c(coords, 'rank_position_group_direction'), by = c(group)] + data.table::set(DT, j = 'zzz_N_by_group', value = NULL) + + return(DT[]) } From eafd6e781595707ecabb89836cb6214d823978d8 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:56:24 -0300 Subject: [PATCH 17/63] fst test distance to leader --- tests/testthat/test-distance-to-leader.R | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 tests/testthat/test-distance-to-leader.R diff --git a/tests/testthat/test-distance-to-leader.R b/tests/testthat/test-distance-to-leader.R new file mode 100644 index 00000000..a8cb7110 --- /dev/null +++ b/tests/testthat/test-distance-to-leader.R @@ -0,0 +1,2 @@ +# Test distance_to_leader +context('test distance_to_leader') From 1c2e639971a7e193b0004cda3dad3f497d96bb03 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:56:28 -0300 Subject: [PATCH 18/63] fix missing group arg --- R/distance_to_leader.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index e3d13447..26f6f6d0 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -74,7 +74,7 @@ #' leader_direction_group(DT, coords = c('X', 'Y')) #' #' # Calculate distance to leader -#' distance_to_leader(DT, coords = c('X', 'Y')) +#' distance_to_leader(DT, coords = c('X', 'Y'), group = 'group') distance_to_leader <- function( DT = NULL, coords = NULL, From 21e491ba2abeb20e795c35fe72e2d7809baeb07e Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 19:56:41 -0300 Subject: [PATCH 19/63] setup tests --- tests/testthat/test-distance-to-leader.R | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/testthat/test-distance-to-leader.R b/tests/testthat/test-distance-to-leader.R index a8cb7110..062fbf25 100644 --- a/tests/testthat/test-distance-to-leader.R +++ b/tests/testthat/test-distance-to-leader.R @@ -1,2 +1,25 @@ # Test distance_to_leader context('test distance_to_leader') + +library(spatsoc) + +DT <- fread('../testdata/DT.csv') +id <- 'ID' +datetime <- 'datetime' +timethreshold <- '20 minutes' +threshold <- 50 +coords <- c('X', 'Y') +timegroup <- 'timegroup' +group <- 'group' +projection <- 32736 + +DT[, datetime := as.POSIXct(datetime, tz = 'UTC')] +group_times(DT, datetime = datetime, timethreshold) +group_pts(DT, threshold = threshold, id = id, + coords = coords, timegroup = timegroup) +centroid_group(DT, coords = coords, group = group, na.rm = TRUE) +direction_step(DT = DT, id = id, coords = coords, projection = projection) +direction_group(DT) +leader_direction_group(DT, coords = coords, group = group, return_rank = TRUE) + +clean_DT <- copy(DT) From 749bf77736185cdeb64924bc23b338c8d2532870 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:07:16 -0300 Subject: [PATCH 20/63] fix rm group with missing leader --- tests/testthat/test-distance-to-leader.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/testthat/test-distance-to-leader.R b/tests/testthat/test-distance-to-leader.R index 062fbf25..8f8c1ca8 100644 --- a/tests/testthat/test-distance-to-leader.R +++ b/tests/testthat/test-distance-to-leader.R @@ -22,4 +22,7 @@ direction_step(DT = DT, id = id, coords = coords, projection = projection) direction_group(DT) leader_direction_group(DT, coords = coords, group = group, return_rank = TRUE) +# Removing group with missing leader +DT <- copy(DT)[group != 868] + clean_DT <- copy(DT) From 2e3f5fbfd572b8ba37dbcf8030090fc42e03dd04 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:07:26 -0300 Subject: [PATCH 21/63] test required args --- tests/testthat/test-distance-to-leader.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/testthat/test-distance-to-leader.R b/tests/testthat/test-distance-to-leader.R index 8f8c1ca8..d2985fee 100644 --- a/tests/testthat/test-distance-to-leader.R +++ b/tests/testthat/test-distance-to-leader.R @@ -26,3 +26,15 @@ leader_direction_group(DT, coords = coords, group = group, return_rank = TRUE) DT <- copy(DT)[group != 868] clean_DT <- copy(DT) + +test_that('DT is required', { + expect_error(distance_to_leader(DT = NULL)) +}) + +test_that('arguments required, otherwise error detected', { + expect_error(distance_to_leader(DT, coords = NULL), + 'coords req') + expect_error(distance_to_leader(DT, coords = coords, group = NULL), + 'group column name required') +}) + From d90d59990271f9a3a449e30d21096f86f8db7dc3 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:07:31 -0300 Subject: [PATCH 22/63] test colnames exist --- tests/testthat/test-distance-to-leader.R | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/testthat/test-distance-to-leader.R b/tests/testthat/test-distance-to-leader.R index d2985fee..c0982a2d 100644 --- a/tests/testthat/test-distance-to-leader.R +++ b/tests/testthat/test-distance-to-leader.R @@ -38,3 +38,13 @@ test_that('arguments required, otherwise error detected', { 'group column name required') }) +test_that('column names must exist in DT', { + expect_error(distance_to_leader(DT, coords = rep('potato', 2), group = group), + 'potato field') + expect_error(distance_to_leader(DT, coords = coords, group = 'potato'), + 'group column') + copy_DT <- copy(DT) + setnames(copy_DT, 'rank_position_group_direction', 'potato') + expect_error(distance_to_leader(copy_DT, coords = coords, group = group), + 'did you run leader?') +}) From 554870a4fc9510f0d1253499d6683cbde5de10b3 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:07:35 -0300 Subject: [PATCH 23/63] test coords --- tests/testthat/test-distance-to-leader.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/testthat/test-distance-to-leader.R b/tests/testthat/test-distance-to-leader.R index c0982a2d..9e5ab35e 100644 --- a/tests/testthat/test-distance-to-leader.R +++ b/tests/testthat/test-distance-to-leader.R @@ -48,3 +48,15 @@ test_that('column names must exist in DT', { expect_error(distance_to_leader(copy_DT, coords = coords, group = group), 'did you run leader?') }) + +test_that('coords are correctly provided or error detected', { + expect_error(distance_to_leader(DT, coords = c('X', NULL), group = group), + 'coords requires a vector') + copy_DT <- copy(DT)[, X := as.character(X)] + expect_error(distance_to_leader(copy_DT, coords = coords, group = group), + 'coords must be numeric') + copy_DT <- copy(DT)[, X := as.character(X)] + expect_error(distance_to_leader(copy_DT, coords = coords, + group = group), + 'coords must be numeric') +}) From 50cc1a714cca206de4939a527483724046187cbe Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:08:10 -0300 Subject: [PATCH 24/63] test overwrite --- tests/testthat/test-distance-to-leader.R | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/testthat/test-distance-to-leader.R b/tests/testthat/test-distance-to-leader.R index 9e5ab35e..1c3d38a0 100644 --- a/tests/testthat/test-distance-to-leader.R +++ b/tests/testthat/test-distance-to-leader.R @@ -60,3 +60,11 @@ test_that('coords are correctly provided or error detected', { group = group), 'coords must be numeric') }) + +test_that('message when distance_leader column overwritten', { + copyDT <- copy(clean_DT)[, distance_leader := 1] + expect_message( + distance_to_leader(copyDT, coords = coords, group = group), + 'distance_leader column will be overwritten' + ) +}) From c634b90ea6fee7c64c634e426be251a61a445910 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:08:15 -0300 Subject: [PATCH 25/63] test dims --- tests/testthat/test-distance-to-leader.R | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/testthat/test-distance-to-leader.R b/tests/testthat/test-distance-to-leader.R index 1c3d38a0..c9aa8db5 100644 --- a/tests/testthat/test-distance-to-leader.R +++ b/tests/testthat/test-distance-to-leader.R @@ -68,3 +68,18 @@ test_that('message when distance_leader column overwritten', { 'distance_leader column will be overwritten' ) }) + +test_that('no rows are added to the result DT', { + copyDT <- copy(clean_DT) + + expect_equal(nrow(copyDT), + nrow(distance_to_leader(copyDT, coords = coords, group = group))) +}) + +test_that('one column added to the result DT', { + copyDT <- copy(clean_DT) + + expect_equal(ncol(copyDT) + 1, + ncol(distance_to_leader(copyDT, coords = coords, group = group))) +}) + From 108a625d678271518a1e5e3c6ef2fb1e048ed2b3 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:08:21 -0300 Subject: [PATCH 26/63] test return types --- tests/testthat/test-distance-to-leader.R | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/testthat/test-distance-to-leader.R b/tests/testthat/test-distance-to-leader.R index c9aa8db5..71aa7903 100644 --- a/tests/testthat/test-distance-to-leader.R +++ b/tests/testthat/test-distance-to-leader.R @@ -83,3 +83,14 @@ test_that('one column added to the result DT', { ncol(distance_to_leader(copyDT, coords = coords, group = group))) }) +test_that('column added to the result DT is a double', { + expect_type( + distance_to_leader(DT, coords = coords, group = group)$distance_leader, + 'double' + ) +}) + +test_that('returns a data.table', { + expect_s3_class(distance_to_leader(DT, coords = coords, group = group), + 'data.table') +}) From bb91dc77e4edf5bca9de129d52f485da2a2c97d3 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:08:39 -0300 Subject: [PATCH 27/63] fix missing group arg --- tests/testthat/test-distance-to-leader.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-distance-to-leader.R b/tests/testthat/test-distance-to-leader.R index 71aa7903..55e96d8f 100644 --- a/tests/testthat/test-distance-to-leader.R +++ b/tests/testthat/test-distance-to-leader.R @@ -32,7 +32,7 @@ test_that('DT is required', { }) test_that('arguments required, otherwise error detected', { - expect_error(distance_to_leader(DT, coords = NULL), + expect_error(distance_to_leader(DT, coords = NULL, group = group), 'coords req') expect_error(distance_to_leader(DT, coords = coords, group = NULL), 'group column name required') From e460b8f720b06b58ec2119dad9e9b20650e07a65 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:08:58 -0300 Subject: [PATCH 28/63] reorg check --- R/distance_to_leader.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index 26f6f6d0..77823cc9 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -86,14 +86,14 @@ distance_to_leader <- function( stop('input DT required') } - if (length(coords) != 2) { - stop('coords requires a vector of column names for coordinates X and Y') - } - if (is.null(group)) { stop('group column name required') } + if (length(coords) != 2) { + stop('coords requires a vector of column names for coordinates X and Y') + } + if (!group %in% colnames(DT)) { stop('group column not present in input DT, did you run group_pts?') } From 33b177cf59efdd234bfcddfd01ba32fe63cd59df Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:16:28 -0300 Subject: [PATCH 29/63] check if rank pos is numeric --- R/distance_to_leader.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index 77823cc9..29bccbde 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -114,7 +114,6 @@ distance_to_leader <- function( stop('coords must be numeric') } - # check exists, is numeric leader_col <- 'rank_position_group_direction' if (!leader_col %in% colnames(DT)) { @@ -124,6 +123,10 @@ distance_to_leader <- function( 'did you run leader_direction_group(return_rank = TRUE)?')) } + if (!is.numeric(DT[[leader_col]]) { + stop(paste0(leader_col, ' column must be numeric')) + }) + out_col <- 'distance_leader' if (out_col %in% colnames(DT)) { message( @@ -132,7 +135,6 @@ distance_to_leader <- function( data.table::set(DT, j = out_col, value = NULL) } - DT[, zzz_N_by_group := .N, by = c(group)] check_has_leader <- DT[, .( From 8ead81e1611290161a6bdd4f0e642902d4caaff2 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:22:10 -0300 Subject: [PATCH 30/63] rename to direction_to_leader --- R/bearing_to_leader.R | 39 --------------------------------------- R/direction_to_leader.R | 1 + 2 files changed, 1 insertion(+), 39 deletions(-) delete mode 100644 R/bearing_to_leader.R create mode 100644 R/direction_to_leader.R diff --git a/R/bearing_to_leader.R b/R/bearing_to_leader.R deleted file mode 100644 index ea86f71e..00000000 --- a/R/bearing_to_leader.R +++ /dev/null @@ -1,39 +0,0 @@ -#' Calculate absolute bearing to group leader -#' -#' Leader as identified using group_leader by first position along -#' group axis of movement (return_rank = TRUE) -#' -#' Note: coords must be in order x, y -#' -#' @param DT -#' @param coords character vector columns names for x and y -#' @param group group column name generated by eg. group_pts -bearing_to_leader <- function(DT, coords = c('x', 'y'), group = 'group') { - stopifnot(first(coords) %in% colnames(DT)) - stopifnot(last(coords) %in% colnames(DT)) - stopifnot(group %in% colnames(DT)) - stopifnot('rank_dist_along_group_bearing' %in% colnames(DT)) - - check_has_leader <- DT[, .(has_leader = any(rank_dist_along_group_bearing == 1)), - by = c(group)][!(has_leader)] - - if (check_has_leader[, .N > 0]) { - warning('groups found missing leader (rank_dist_along_group_bearing == 1): \n', - check_has_leader[, paste(group, collapse = ', ')]) - } - - DT[, temp_leader_xcol := .SD[which(rank_dist_along_group_bearing == 1)], - .SDcols = first(coords), by = c(group)] - DT[, temp_leader_ycol := .SD[which(rank_dist_along_group_bearing == 1)], - .SDcols = last(coords), by = c(group)] - - DT[!group %in% check_has_leader$group, bearing_leader := fifelse( - .SD[[first(coords)]] == .SD[['temp_leader_xcol']] & - .SD[[last(coords)]] == .SD[['temp_leader_ycol']], - NaN, - atan2(.SD[['temp_leader_ycol']] - .SD[[last(coords)]], - (.SD[['temp_leader_xcol']] - .SD[[first(coords)]])) - )] - DT[, c('temp_leader_xcol', 'temp_leader_ycol') := NULL] - return(DT) -} diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R new file mode 100644 index 00000000..fc2929fb --- /dev/null +++ b/R/direction_to_leader.R @@ -0,0 +1 @@ +#' Direction to group leader From b9db446fbf63e5d5cd2094108e28837d227bfbbd Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:22:14 -0300 Subject: [PATCH 31/63] description --- R/direction_to_leader.R | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R index fc2929fb..8ae71b7a 100644 --- a/R/direction_to_leader.R +++ b/R/direction_to_leader.R @@ -1 +1,10 @@ #' Direction to group leader +#' +#' \code{direction_to_leader} calculates the direction to the leader of each +#' spatiotemporal group. The function accepts a \code{data.table} with +#' relocation data appended with a \code{rank_position_group_direction} column +#' indicating the ranked position along the group direction generated with +#' \code{leader_direction_group(return_rank = TRUE)}. Relocation data should be +#' in planar coordinates provided in two columns representing the X and Y +#' coordinates. +#' From 95949ba11e30789ef1203fccbe674719da8ddb1a Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:22:17 -0300 Subject: [PATCH 32/63] details --- R/direction_to_leader.R | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R index 8ae71b7a..3b308346 100644 --- a/R/direction_to_leader.R +++ b/R/direction_to_leader.R @@ -8,3 +8,14 @@ #' in planar coordinates provided in two columns representing the X and Y #' coordinates. #' +#' The \code{DT} must be a \code{data.table}. If your data is a +#' \code{data.frame}, you can convert it by reference using +#' \code{\link[data.table:setDT]{data.table::setDT}} or by reassigning using +#' \code{\link[data.table:data.table]{data.table::data.table}}. +#' +#' This function expects a \code{rank_position_group_direction} column +#' generated with \code{leader_direction_group(return_rank = TRUE)}, +#' a \code{group} column generated with the +#' \code{group_pts} function. The \code{coords} and \code{group} arguments +#' expect the names of columns in \code{DT} which correspond to the X and Y +#' coordinates and group columns. From 40e82b3f1b4ff1e950ca9821d9c8fe596cff5b6a Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:22:19 -0300 Subject: [PATCH 33/63] params --- R/direction_to_leader.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R index 3b308346..de20b116 100644 --- a/R/direction_to_leader.R +++ b/R/direction_to_leader.R @@ -19,3 +19,5 @@ #' \code{group_pts} function. The \code{coords} and \code{group} arguments #' expect the names of columns in \code{DT} which correspond to the X and Y #' coordinates and group columns. +#' +#' @inheritParams leader_direction_group From 90863288d24e997a913af2c2e258b379c95065eb Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:22:22 -0300 Subject: [PATCH 34/63] return --- R/direction_to_leader.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R index de20b116..988d5c04 100644 --- a/R/direction_to_leader.R +++ b/R/direction_to_leader.R @@ -21,3 +21,9 @@ #' coordinates and group columns. #' #' @inheritParams leader_direction_group +#' +#' @return \code{direction_to_leader} returns the input \code{DT} appended with +#' a \code{direction_leader} column indicating the direction to the group leader. +#' +#' A message is returned when the \code{direction_leader} column is already exist in the input \code{DT} +#' because it will be overwritten. From 004cccdec7f2279346d0c750af1737cc78453e47 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:22:29 -0300 Subject: [PATCH 35/63] export, family, seealso, references --- R/direction_to_leader.R | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R index 988d5c04..63c917f3 100644 --- a/R/direction_to_leader.R +++ b/R/direction_to_leader.R @@ -27,3 +27,13 @@ #' #' A message is returned when the \code{direction_leader} column is already exist in the input \code{DT} #' because it will be overwritten. +#' +#' @export +#' @family Direction functions +#' @seealso [distance_to_leader], [leader_direction_group], [group_pts] +#' @references +#' +#' See examples of using direction to leader and position within group: +#' * +#' * +#' * From b8976ddb8d4b7dd6027bba8ddde381ff9db989a5 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:22:38 -0300 Subject: [PATCH 36/63] example --- R/direction_to_leader.R | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R index 63c917f3..64d567cf 100644 --- a/R/direction_to_leader.R +++ b/R/direction_to_leader.R @@ -37,3 +37,41 @@ #' * #' * #' * +#' +#' @examples +#' # Load data.table +#' library(data.table) +#' \dontshow{data.table::setDTthreads(1)} +#' +#' # Read example data +#' DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc")) +#' +#' # Cast the character column to POSIXct +#' DT[, datetime := as.POSIXct(datetime, tz = 'UTC')] +#' +#' # Temporal grouping +#' group_times(DT, datetime = 'datetime', threshold = '20 minutes') +#' +#' # Spatial grouping with timegroup +#' group_pts(DT, threshold = 50, id = 'ID', +#' coords = c('X', 'Y'), timegroup = 'timegroup') +#' +#' # Calculate direction at each step +#' direction_step( +#' DT = DT, +#' id = 'ID', +#' coords = c('X', 'Y'), +#' projection = 32736 +#' ) +#' +#' # Calculate group centroid +#' centroid_group(DT, coords = c('X', 'Y')) +#' +#' # Calculate group direction +#' direction_group(DT) +#' +#' # Calculate leader in terms of position along group direction +#' leader_direction_group(DT, coords = c('X', 'Y')) +#' +#' # Calculate direction to leader +#' direction_to_leader(DT, coords = c('X', 'Y'), group = 'group') From 0111c5b28735143fc639525e27c67c8a02dfbf52 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:22:42 -0300 Subject: [PATCH 37/63] args --- R/direction_to_leader.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R index 64d567cf..64f0c236 100644 --- a/R/direction_to_leader.R +++ b/R/direction_to_leader.R @@ -75,3 +75,7 @@ #' #' # Calculate direction to leader #' direction_to_leader(DT, coords = c('X', 'Y'), group = 'group') +direction_to_leader <- function( + DT = NULL, + coords = NULL, + group = NULL) { From 60445a7736f8de254f13033d39bf94dbf5586c48 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:23:35 -0300 Subject: [PATCH 38/63] fix check type leader col --- R/distance_to_leader.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index 29bccbde..8af26469 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -123,9 +123,10 @@ distance_to_leader <- function( 'did you run leader_direction_group(return_rank = TRUE)?')) } - if (!is.numeric(DT[[leader_col]]) { + if (!is.numeric(DT[[leader_col]])) { stop(paste0(leader_col, ' column must be numeric')) - }) + } + out_col <- 'distance_leader' if (out_col %in% colnames(DT)) { From 95329b2dd33dec87c427212bbe69741741074a60 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:23:51 -0300 Subject: [PATCH 39/63] also seealso direction to leader --- R/distance_to_leader.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index 8af26469..f76cad72 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -30,7 +30,7 @@ #' #' @export #' @family Distance functions -#' @seealso [leader_direction_group], [group_pts] +#' @seealso [direction_to_leader], [leader_direction_group], [group_pts] #' @references #' #' See examples of using distance to leader and position within group: From e35d512eee23ef918b736aa1414f3969b17beffd Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:32:24 -0300 Subject: [PATCH 40/63] fix nse notes --- R/direction_to_leader.R | 3 +++ R/distance_to_leader.R | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R index 64f0c236..2ab309bc 100644 --- a/R/direction_to_leader.R +++ b/R/direction_to_leader.R @@ -79,3 +79,6 @@ direction_to_leader <- function( DT = NULL, coords = NULL, group = NULL) { + # Due to NSE notes + direction_leader <- rank_position_group_direction <- has_leader <- + zzz_N_by_group <- . <- NULL diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index f76cad72..3b1b57ee 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -80,7 +80,8 @@ distance_to_leader <- function( coords = NULL, group = NULL) { # Due to NSE notes - # distance_leader <- zzz_N_by_group <- NULL + distance_leader <- zzz_N_by_group <- rank_position_group_direction <- + has_leader <- . <- NULL if (is.null(DT)) { stop('input DT required') From ac74c1bc3f53f4e7e2e1380fdc988fd905889356 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:32:46 -0300 Subject: [PATCH 41/63] checks --- R/direction_to_leader.R | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R index 2ab309bc..09b0f5b6 100644 --- a/R/direction_to_leader.R +++ b/R/direction_to_leader.R @@ -82,3 +82,56 @@ direction_to_leader <- function( # Due to NSE notes direction_leader <- rank_position_group_direction <- has_leader <- zzz_N_by_group <- . <- NULL + + if (is.null(DT)) { + stop('input DT required') + } + + if (is.null(group)) { + stop('group column name required') + } + + if (length(coords) != 2) { + stop('coords requires a vector of column names for coordinates X and Y') + } + + if (!group %in% colnames(DT)) { + stop('group column not present in input DT, did you run group_pts?') + } + + check_cols <- c(coords, group) + + if (any(!(check_cols %in% colnames(DT)))) { + stop(paste0( + as.character(paste(setdiff( + check_cols, + colnames(DT) + ), collapse = ', ')), + ' field(s) provided are not present in input DT' + )) + } + + if (any(!(DT[, vapply(.SD, is.numeric, TRUE), .SDcols = coords]))) { + stop('coords must be numeric') + } + + leader_col <- 'rank_position_group_direction' + + if (!leader_col %in% colnames(DT)) { + stop(paste0( + leader_col, + ' column not present in input DT, ', + 'did you run leader_direction_group(return_rank = TRUE)?')) + } + + if (!is.numeric(DT[[leader_col]])) { + stop(paste0(leader_col, ' column must be numeric')) + } + + out_col <- 'direction_leader' + if (out_col %in% colnames(DT)) { + message( + paste0(out_col, ' column will be overwritten by this function') + ) + data.table::set(DT, j = out_col, value = NULL) + } From b78394cd5e0cb9415311d06c81933604a1b4be3a Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:32:56 -0300 Subject: [PATCH 42/63] warn if group is missing leader --- R/direction_to_leader.R | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R index 09b0f5b6..8b78ef90 100644 --- a/R/direction_to_leader.R +++ b/R/direction_to_leader.R @@ -135,3 +135,14 @@ direction_to_leader <- function( ) data.table::set(DT, j = out_col, value = NULL) } + + check_has_leader <- DT[, .( + has_leader = any(rank_position_group_direction == 1)), + by = c(group)][!(has_leader)] + + if (check_has_leader[, .N > 0]) { + warning( + 'groups found missing leader (rank_position_group_direction == 1): \n', + check_has_leader[, paste(group, collapse = ', ')] + ) + } From 5a13336365dbb269bb38408633e5ce111983da41 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:33:03 -0300 Subject: [PATCH 43/63] processing --- R/direction_to_leader.R | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R index 8b78ef90..d43fac44 100644 --- a/R/direction_to_leader.R +++ b/R/direction_to_leader.R @@ -146,3 +146,22 @@ direction_to_leader <- function( check_has_leader[, paste(group, collapse = ', ')] ) } + + zzz_leader_coords <- c('zzz_leader_xcol', 'zzz_leader_ycol') + DT[, c(zzz_leader_coords) := + .SD[which(rank_position_group_direction == 1)], + .SDcols = c(coords), + by = c(group)] + + DT[!group %in% check_has_leader$group, direction_leader := fifelse( + .SD[[1]] == .SD[[3]] & + .SD[[2]] == .SD[[4]], + NaN, + atan2(.SD[[4]] - .SD[[2]], (.SD[[3]] - .SD[[1]])) + ), + .SDcols = c(coords, zzz_leader_coords)] + + data.table::set(DT, j = zzz_leader_coords, value = NULL) + + return(DT[]) +} From d027a5fd023cb6d4f457e77b7c97fc63461f14cf Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:33:11 -0300 Subject: [PATCH 44/63] fst test-direction-to-leader --- tests/testthat/test-direction-to-leader.R | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 tests/testthat/test-direction-to-leader.R diff --git a/tests/testthat/test-direction-to-leader.R b/tests/testthat/test-direction-to-leader.R new file mode 100644 index 00000000..a8cb7110 --- /dev/null +++ b/tests/testthat/test-direction-to-leader.R @@ -0,0 +1,2 @@ +# Test distance_to_leader +context('test distance_to_leader') From f9ccf186a5496fb1719bafce73dc76a7c704e80f Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:33:16 -0300 Subject: [PATCH 45/63] setup --- tests/testthat/test-direction-to-leader.R | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/testthat/test-direction-to-leader.R b/tests/testthat/test-direction-to-leader.R index a8cb7110..16b461c8 100644 --- a/tests/testthat/test-direction-to-leader.R +++ b/tests/testthat/test-direction-to-leader.R @@ -1,2 +1,23 @@ # Test distance_to_leader context('test distance_to_leader') + +library(spatsoc) + +DT <- fread('../testdata/DT.csv') +id <- 'ID' +datetime <- 'datetime' +timethreshold <- '20 minutes' +threshold <- 50 +coords <- c('X', 'Y') +timegroup <- 'timegroup' +group <- 'group' +projection <- 32736 + +DT[, datetime := as.POSIXct(datetime, tz = 'UTC')] +group_times(DT, datetime = datetime, timethreshold) +group_pts(DT, threshold = threshold, id = id, + coords = coords, timegroup = timegroup) +centroid_group(DT, coords = coords, group = group, na.rm = TRUE) +direction_step(DT = DT, id = id, coords = coords, projection = projection) +direction_group(DT) +leader_direction_group(DT, coords = coords, group = group, return_rank = TRUE) From 02e80b014b0d2bf5757e0e125b5ab882f64fddb2 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:33:35 -0300 Subject: [PATCH 46/63] fix rm group with missing leader --- tests/testthat/test-direction-to-leader.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/testthat/test-direction-to-leader.R b/tests/testthat/test-direction-to-leader.R index 16b461c8..62d87df5 100644 --- a/tests/testthat/test-direction-to-leader.R +++ b/tests/testthat/test-direction-to-leader.R @@ -21,3 +21,6 @@ centroid_group(DT, coords = coords, group = group, na.rm = TRUE) direction_step(DT = DT, id = id, coords = coords, projection = projection) direction_group(DT) leader_direction_group(DT, coords = coords, group = group, return_rank = TRUE) + +# Removing group with missing leader +DT <- copy(DT)[group != 868] From 47473a3581943cf4d25eadddbbbcf5b608279bdf Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:33:44 -0300 Subject: [PATCH 47/63] test required --- tests/testthat/test-direction-to-leader.R | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/testthat/test-direction-to-leader.R b/tests/testthat/test-direction-to-leader.R index 62d87df5..e8715b21 100644 --- a/tests/testthat/test-direction-to-leader.R +++ b/tests/testthat/test-direction-to-leader.R @@ -24,3 +24,17 @@ leader_direction_group(DT, coords = coords, group = group, return_rank = TRUE) # Removing group with missing leader DT <- copy(DT)[group != 868] + +clean_DT <- copy(DT) + +test_that('DT is required', { + expect_error(distance_to_leader(DT = NULL)) +}) + +test_that('arguments required, otherwise error detected', { + expect_error(distance_to_leader(DT, coords = NULL, group = group), + 'coords req') + expect_error(distance_to_leader(DT, coords = coords, group = NULL), + 'group column name required') +}) + From ded3df6802972c696a1b284497978db15ac2d1d8 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:33:48 -0300 Subject: [PATCH 48/63] test colnames --- tests/testthat/test-direction-to-leader.R | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/testthat/test-direction-to-leader.R b/tests/testthat/test-direction-to-leader.R index e8715b21..a800b51b 100644 --- a/tests/testthat/test-direction-to-leader.R +++ b/tests/testthat/test-direction-to-leader.R @@ -38,3 +38,14 @@ test_that('arguments required, otherwise error detected', { 'group column name required') }) +test_that('column names must exist in DT', { + expect_error(distance_to_leader(DT, coords = rep('potato', 2), group = group), + 'potato field') + expect_error(distance_to_leader(DT, coords = coords, group = 'potato'), + 'group column') + copy_DT <- copy(DT) + setnames(copy_DT, 'rank_position_group_direction', 'potato') + expect_error(distance_to_leader(copy_DT, coords = coords, group = group), + 'did you run leader?') +}) + From 8287e9782c430dd12d02df28b10518fb44c8f45a Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:33:52 -0300 Subject: [PATCH 49/63] test coords --- tests/testthat/test-direction-to-leader.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/testthat/test-direction-to-leader.R b/tests/testthat/test-direction-to-leader.R index a800b51b..7f3d8a31 100644 --- a/tests/testthat/test-direction-to-leader.R +++ b/tests/testthat/test-direction-to-leader.R @@ -49,3 +49,15 @@ test_that('column names must exist in DT', { 'did you run leader?') }) +test_that('coords are correctly provided or error detected', { + expect_error(distance_to_leader(DT, coords = c('X', NULL), group = group), + 'coords requires a vector') + copy_DT <- copy(DT)[, X := as.character(X)] + expect_error(distance_to_leader(copy_DT, coords = coords, group = group), + 'coords must be numeric') + copy_DT <- copy(DT)[, X := as.character(X)] + expect_error(distance_to_leader(copy_DT, coords = coords, + group = group), + 'coords must be numeric') +}) + From bf390d13f6a590e4a04b40a38904e613143eaa2c Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:33:55 -0300 Subject: [PATCH 50/63] test overwrite --- tests/testthat/test-direction-to-leader.R | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/testthat/test-direction-to-leader.R b/tests/testthat/test-direction-to-leader.R index 7f3d8a31..5245006b 100644 --- a/tests/testthat/test-direction-to-leader.R +++ b/tests/testthat/test-direction-to-leader.R @@ -61,3 +61,10 @@ test_that('coords are correctly provided or error detected', { 'coords must be numeric') }) +test_that('message when distance_leader column overwritten', { + copyDT <- copy(clean_DT)[, distance_leader := 1] + expect_message( + distance_to_leader(copyDT, coords = coords, group = group), + 'distance_leader column will be overwritten' + ) +}) From 392b80cf1e4baa54b5dda31537f11eba2f99167b Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:34:00 -0300 Subject: [PATCH 51/63] test return dims --- tests/testthat/test-direction-to-leader.R | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/testthat/test-direction-to-leader.R b/tests/testthat/test-direction-to-leader.R index 5245006b..904c2a9b 100644 --- a/tests/testthat/test-direction-to-leader.R +++ b/tests/testthat/test-direction-to-leader.R @@ -68,3 +68,18 @@ test_that('message when distance_leader column overwritten', { 'distance_leader column will be overwritten' ) }) + +test_that('no rows are added to the result DT', { + copyDT <- copy(clean_DT) + + expect_equal(nrow(copyDT), + nrow(distance_to_leader(copyDT, coords = coords, group = group))) +}) + +test_that('one column added to the result DT', { + copyDT <- copy(clean_DT) + + expect_equal(ncol(copyDT) + 1, + ncol(distance_to_leader(copyDT, coords = coords, group = group))) +}) + From 58e46e7af0465925e5b3e6ba7b734a46e308eae0 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:34:04 -0300 Subject: [PATCH 52/63] test return types --- tests/testthat/test-direction-to-leader.R | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/testthat/test-direction-to-leader.R b/tests/testthat/test-direction-to-leader.R index 904c2a9b..55e96d8f 100644 --- a/tests/testthat/test-direction-to-leader.R +++ b/tests/testthat/test-direction-to-leader.R @@ -83,3 +83,14 @@ test_that('one column added to the result DT', { ncol(distance_to_leader(copyDT, coords = coords, group = group))) }) +test_that('column added to the result DT is a double', { + expect_type( + distance_to_leader(DT, coords = coords, group = group)$distance_leader, + 'double' + ) +}) + +test_that('returns a data.table', { + expect_s3_class(distance_to_leader(DT, coords = coords, group = group), + 'data.table') +}) From eb34f7858aa80e2160b3304ef500ef88c92a298e Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Thu, 31 Oct 2024 20:39:08 -0300 Subject: [PATCH 53/63] test if rank pos missing errors --- tests/testthat/test-direction-to-leader.R | 3 +++ tests/testthat/test-distance-to-leader.R | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/tests/testthat/test-direction-to-leader.R b/tests/testthat/test-direction-to-leader.R index 55e96d8f..ff85b76a 100644 --- a/tests/testthat/test-direction-to-leader.R +++ b/tests/testthat/test-direction-to-leader.R @@ -59,6 +59,9 @@ test_that('coords are correctly provided or error detected', { expect_error(distance_to_leader(copy_DT, coords = coords, group = group), 'coords must be numeric') + copy_DT <- copy(DT)[, rank_position_group_direction := NULL] + expect_error(distance_to_leader(copy_DT, coords = coords, + group = group)) }) test_that('message when distance_leader column overwritten', { diff --git a/tests/testthat/test-distance-to-leader.R b/tests/testthat/test-distance-to-leader.R index 55e96d8f..ec41dd45 100644 --- a/tests/testthat/test-distance-to-leader.R +++ b/tests/testthat/test-distance-to-leader.R @@ -59,6 +59,10 @@ test_that('coords are correctly provided or error detected', { expect_error(distance_to_leader(copy_DT, coords = coords, group = group), 'coords must be numeric') + + copy_DT <- copy(DT)[, rank_position_group_direction := NULL] + expect_error(distance_to_leader(copy_DT, coords = coords, + group = group)) }) test_that('message when distance_leader column overwritten', { From 09cc1b20e54aed93efddaf5485eacacd473da381 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 1 Nov 2024 10:45:19 -0300 Subject: [PATCH 54/63] fix examples use return rank = TRUE --- R/direction_to_leader.R | 7 ++++++- R/distance_to_leader.R | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R index d43fac44..3b85175a 100644 --- a/R/direction_to_leader.R +++ b/R/direction_to_leader.R @@ -71,7 +71,12 @@ #' direction_group(DT) #' #' # Calculate leader in terms of position along group direction -#' leader_direction_group(DT, coords = c('X', 'Y')) +#' leader_direction_group( +#' DT, +#' coords = c('X', 'Y'), +#' group = 'group', +#' return_rank = TRUE +#' ) #' #' # Calculate direction to leader #' direction_to_leader(DT, coords = c('X', 'Y'), group = 'group') diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index 3b1b57ee..1a439847 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -71,7 +71,12 @@ #' direction_group(DT) #' #' # Calculate leader in terms of position along group direction -#' leader_direction_group(DT, coords = c('X', 'Y')) +#' leader_direction_group( +#' DT, +#' coords = c('X', 'Y'), +#' group = 'group', +#' return_rank = TRUE +#' ) #' #' # Calculate distance to leader #' distance_to_leader(DT, coords = c('X', 'Y'), group = 'group') From 15ad87a3fb2bbdd0bfd74a2c65b175eea4b31239 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 1 Nov 2024 10:45:30 -0300 Subject: [PATCH 55/63] fix set default group = 'group' --- R/direction_to_leader.R | 2 +- R/distance_to_leader.R | 2 +- R/leader_direction_group.R | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R index 3b85175a..9071fa42 100644 --- a/R/direction_to_leader.R +++ b/R/direction_to_leader.R @@ -83,7 +83,7 @@ direction_to_leader <- function( DT = NULL, coords = NULL, - group = NULL) { + group = 'group') { # Due to NSE notes direction_leader <- rank_position_group_direction <- has_leader <- zzz_N_by_group <- . <- NULL diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index 1a439847..c5b2e635 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -83,7 +83,7 @@ distance_to_leader <- function( DT = NULL, coords = NULL, - group = NULL) { + group = 'group') { # Due to NSE notes distance_leader <- zzz_N_by_group <- rank_position_group_direction <- has_leader <- . <- NULL diff --git a/R/leader_direction_group.R b/R/leader_direction_group.R index d5810840..44e71274 100644 --- a/R/leader_direction_group.R +++ b/R/leader_direction_group.R @@ -92,7 +92,7 @@ leader_direction_group <- function( DT = NULL, group_direction = 'group_direction', coords = NULL, - group = NULL, + group = 'group', return_rank = FALSE, ties.method = 'average') { # Due to NSE notes From 15299cac2c70ce718c2a8c611be82e51fd157f54 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 1 Nov 2024 10:53:14 -0300 Subject: [PATCH 56/63] fix DT param, inheritParams relationships --- R/direction_group.R | 4 ++-- R/direction_to_centroid.R | 1 + R/direction_to_leader.R | 2 +- R/distance_to_centroid.R | 3 ++- R/distance_to_leader.R | 3 +++ R/leader_direction_group.R | 5 ++++- 6 files changed, 13 insertions(+), 5 deletions(-) diff --git a/R/direction_group.R b/R/direction_group.R index 93043c8c..b2391b9c 100644 --- a/R/direction_group.R +++ b/R/direction_group.R @@ -16,8 +16,8 @@ #' direction column is expected in units of radians and the mean calculated with #' [CircStats::circ.mean()]. #' -#' @param DT input data.table with distance column generated by -#' \code{distance_step} and group column generated with \code{group_pts} +#' @param DT input data.table with direction column generated by +#' \code{direction_step} and group column generated with \code{group_pts} #' @param direction character string of direction column name, default #' "direction" #' @param group character string of group column name, default "group" diff --git a/R/direction_to_centroid.R b/R/direction_to_centroid.R index 6788d959..75f9eabc 100644 --- a/R/direction_to_centroid.R +++ b/R/direction_to_centroid.R @@ -18,6 +18,7 @@ #' expect the names of columns in \code{DT} which correspond to the X and Y #' coordinates and group columns. #' +#' @inheritParams distance_to_centroid #' @inheritParams group_pts #' #' @return \code{direction_to_centroid} returns the input \code{DT} appended diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R index 9071fa42..9aa97271 100644 --- a/R/direction_to_leader.R +++ b/R/direction_to_leader.R @@ -20,7 +20,7 @@ #' expect the names of columns in \code{DT} which correspond to the X and Y #' coordinates and group columns. #' -#' @inheritParams leader_direction_group +#' @inheritParams distance_to_leader #' #' @return \code{direction_to_leader} returns the input \code{DT} appended with #' a \code{direction_leader} column indicating the direction to the group leader. diff --git a/R/distance_to_centroid.R b/R/distance_to_centroid.R index b709b5a2..a93c6ca1 100644 --- a/R/distance_to_centroid.R +++ b/R/distance_to_centroid.R @@ -22,7 +22,8 @@ #' \code{data.table::frank}, see details at #' \code{\link[data.table:frank]{?data.table::frank}}. #' -#' @inheritParams group_pts +#' @param DT input data.table with centroid columns generated by eg. +#' \code{centroid_group} #' @param group group column name, generated by \code{group_pts}, default #' 'group' #' @param return_rank boolean if rank distance should also be returned, default diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index c5b2e635..fe0970da 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -20,6 +20,9 @@ #' expect the names of columns in \code{DT} which correspond to the X and Y #' coordinates and group columns. #' +#' @param DT input data.table with 'rank_position_group_direction' column +#' generated by \code{leader_direction_group} and group column generated by +#' \code{group_pts} #' @inheritParams leader_direction_group #' #' @return \code{distance_to_leader} returns the input \code{DT} appended with diff --git a/R/leader_direction_group.R b/R/leader_direction_group.R index 44e71274..2a4dba9b 100644 --- a/R/leader_direction_group.R +++ b/R/leader_direction_group.R @@ -38,8 +38,11 @@ #' \code{rank_position_group_direction} columns already exist in the input #' \code{DT}, because they will be overwritten. #' -#' @inheritParams direction_group +#' @param DT input data.table with group direction columns generated by +#' \code{direction_group} and centroid columns generated by +#' \code{centroid_group} #' @inheritParams distance_to_centroid +#' @inheritParams group_pts #' @param group_direction group_direction column name generated using #' \code{direction_group}, default 'group_direction' #' From 9caa05b7026edf26605ab64a8c063f328ce6f02a Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 1 Nov 2024 11:10:56 -0300 Subject: [PATCH 57/63] rm group arg from example --- R/direction_to_leader.R | 3 +-- R/distance_to_leader.R | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R index 9aa97271..a9e1e89b 100644 --- a/R/direction_to_leader.R +++ b/R/direction_to_leader.R @@ -74,12 +74,11 @@ #' leader_direction_group( #' DT, #' coords = c('X', 'Y'), -#' group = 'group', #' return_rank = TRUE #' ) #' #' # Calculate direction to leader -#' direction_to_leader(DT, coords = c('X', 'Y'), group = 'group') +#' direction_to_leader(DT, coords = c('X', 'Y')) direction_to_leader <- function( DT = NULL, coords = NULL, diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index fe0970da..78795526 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -77,12 +77,11 @@ #' leader_direction_group( #' DT, #' coords = c('X', 'Y'), -#' group = 'group', #' return_rank = TRUE #' ) #' #' # Calculate distance to leader -#' distance_to_leader(DT, coords = c('X', 'Y'), group = 'group') +#' distance_to_leader(DT, coords = c('X', 'Y')) distance_to_leader <- function( DT = NULL, coords = NULL, From 13cde6899c6597e4f923116c01b308596209d13a Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 1 Nov 2024 11:15:02 -0300 Subject: [PATCH 58/63] adapt test distance to test direction --- tests/testthat/test-direction-to-leader.R | 69 ++++++++++++++++------- 1 file changed, 49 insertions(+), 20 deletions(-) diff --git a/tests/testthat/test-direction-to-leader.R b/tests/testthat/test-direction-to-leader.R index ff85b76a..d44c9f99 100644 --- a/tests/testthat/test-direction-to-leader.R +++ b/tests/testthat/test-direction-to-leader.R @@ -1,5 +1,5 @@ -# Test distance_to_leader -context('test distance_to_leader') +# Test direction_to_leader +context('test direction_to_leader') library(spatsoc) @@ -28,47 +28,47 @@ DT <- copy(DT)[group != 868] clean_DT <- copy(DT) test_that('DT is required', { - expect_error(distance_to_leader(DT = NULL)) + expect_error(direction_to_leader(DT = NULL)) }) test_that('arguments required, otherwise error detected', { - expect_error(distance_to_leader(DT, coords = NULL, group = group), + expect_error(direction_to_leader(DT, coords = NULL, group = group), 'coords req') - expect_error(distance_to_leader(DT, coords = coords, group = NULL), + expect_error(direction_to_leader(DT, coords = coords, group = NULL), 'group column name required') }) test_that('column names must exist in DT', { - expect_error(distance_to_leader(DT, coords = rep('potato', 2), group = group), + expect_error(direction_to_leader(DT, coords = rep('potato', 2), group = group), 'potato field') - expect_error(distance_to_leader(DT, coords = coords, group = 'potato'), + expect_error(direction_to_leader(DT, coords = coords, group = 'potato'), 'group column') copy_DT <- copy(DT) setnames(copy_DT, 'rank_position_group_direction', 'potato') - expect_error(distance_to_leader(copy_DT, coords = coords, group = group), + expect_error(direction_to_leader(copy_DT, coords = coords, group = group), 'did you run leader?') }) test_that('coords are correctly provided or error detected', { - expect_error(distance_to_leader(DT, coords = c('X', NULL), group = group), + expect_error(direction_to_leader(DT, coords = c('X', NULL), group = group), 'coords requires a vector') copy_DT <- copy(DT)[, X := as.character(X)] - expect_error(distance_to_leader(copy_DT, coords = coords, group = group), + expect_error(direction_to_leader(copy_DT, coords = coords, group = group), 'coords must be numeric') copy_DT <- copy(DT)[, X := as.character(X)] - expect_error(distance_to_leader(copy_DT, coords = coords, + expect_error(direction_to_leader(copy_DT, coords = coords, group = group), 'coords must be numeric') copy_DT <- copy(DT)[, rank_position_group_direction := NULL] - expect_error(distance_to_leader(copy_DT, coords = coords, + expect_error(direction_to_leader(copy_DT, coords = coords, group = group)) }) -test_that('message when distance_leader column overwritten', { - copyDT <- copy(clean_DT)[, distance_leader := 1] +test_that('message when direction_leader column overwritten', { + copyDT <- copy(clean_DT)[, direction_leader := 1] expect_message( - distance_to_leader(copyDT, coords = coords, group = group), - 'distance_leader column will be overwritten' + direction_to_leader(copyDT, coords = coords, group = group), + 'direction_leader column will be overwritten' ) }) @@ -76,24 +76,53 @@ test_that('no rows are added to the result DT', { copyDT <- copy(clean_DT) expect_equal(nrow(copyDT), - nrow(distance_to_leader(copyDT, coords = coords, group = group))) + nrow(direction_to_leader(copyDT, coords = coords, group = group))) }) test_that('one column added to the result DT', { copyDT <- copy(clean_DT) expect_equal(ncol(copyDT) + 1, - ncol(distance_to_leader(copyDT, coords = coords, group = group))) + ncol(direction_to_leader(copyDT, coords = coords, group = group))) }) test_that('column added to the result DT is a double', { expect_type( - distance_to_leader(DT, coords = coords, group = group)$distance_leader, + direction_to_leader(DT, coords = coords, group = group)$direction_leader, 'double' ) }) test_that('returns a data.table', { - expect_s3_class(distance_to_leader(DT, coords = coords, group = group), + expect_s3_class(direction_to_leader(DT, coords = coords, group = group), 'data.table') }) + +expect_DT <- data.table( + ID = c('A', 'B'), + X = c(0, 10), + Y = c(0, 0), + group_direction = rep(as_units(0, 'rad'), 2), + group = c(1, 1) +) +centroid_group(expect_DT, coords = coords) +leader_direction_group(expect_DT, coords = coords, + return_rank = TRUE, group = group) +direction_to_leader(expect_DT, coords = c('X', 'Y')) + +test_that('expected results for simple case', { + expect_lte( + expect_DT[, max(direction_leader, na.rm = TRUE)], + 10 + ) + + expect_equal( + expect_DT[is.na(direction_leader), .N], + 1 + ) + expect_equal( + expect_DT[is.na(direction_leader), ID], + 'B' + ) +}) + From 36b2c80f217975ef4e227ca77b8551c2952b4d57 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 1 Nov 2024 11:15:10 -0300 Subject: [PATCH 59/63] test expected dataset --- tests/testthat/test-distance-to-leader.R | 31 ++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/testthat/test-distance-to-leader.R b/tests/testthat/test-distance-to-leader.R index ec41dd45..f00c24fa 100644 --- a/tests/testthat/test-distance-to-leader.R +++ b/tests/testthat/test-distance-to-leader.R @@ -98,3 +98,34 @@ test_that('returns a data.table', { expect_s3_class(distance_to_leader(DT, coords = coords, group = group), 'data.table') }) + + +expect_DT <- data.table( + ID = c('A', 'B'), + X = c(0, 10), + Y = c(0, 0), + group_direction = rep(as_units(0, 'rad'), 2), + group = c(1, 1) +) +centroid_group(expect_DT, coords = coords) +leader_direction_group(expect_DT, coords = coords, + return_rank = TRUE, group = group) +distance_to_leader(expect_DT, coords = c('X', 'Y')) + +test_that('expected results for simple case', { + expect_lte( + expect_DT[, max(distance_leader)], + 10 + ) + + expect_gte( + expect_DT[, min(distance_leader)], + 0 + ) + expect_equal( + expect_DT[distance_leader == min(distance_leader), ID], + 'B' + ) +}) + + From 0c051817a569c4997be97e345c95fd1b22434996 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 1 Nov 2024 11:20:16 -0300 Subject: [PATCH 60/63] test zzz columns are not in result --- tests/testthat/test-direction-to-leader.R | 8 ++++++++ tests/testthat/test-distance-to-leader.R | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/tests/testthat/test-direction-to-leader.R b/tests/testthat/test-direction-to-leader.R index d44c9f99..9dced628 100644 --- a/tests/testthat/test-direction-to-leader.R +++ b/tests/testthat/test-direction-to-leader.R @@ -93,6 +93,14 @@ test_that('column added to the result DT is a double', { ) }) +test_that('zzz columns not added to the result', { + zzz_cols <- c('has_leader', 'zzz_leader_xcol', 'zzz_leader_ycol') + + expect_false( + any(zzz_cols %in% colnames(direction_to_leader(DT, coords = coords))) + ) +}) + test_that('returns a data.table', { expect_s3_class(direction_to_leader(DT, coords = coords, group = group), 'data.table') diff --git a/tests/testthat/test-distance-to-leader.R b/tests/testthat/test-distance-to-leader.R index f00c24fa..844b8b29 100644 --- a/tests/testthat/test-distance-to-leader.R +++ b/tests/testthat/test-distance-to-leader.R @@ -94,6 +94,14 @@ test_that('column added to the result DT is a double', { ) }) +test_that('zzz columns not added to the result', { + zzz_cols <- c('has_leader', 'zzz_N_by_group') + + expect_false( + any(zzz_cols %in% colnames(direction_to_leader(DT, coords = coords))) + ) +}) + test_that('returns a data.table', { expect_s3_class(distance_to_leader(DT, coords = coords, group = group), 'data.table') From 99b9233764346d63870d9c0196a4866a40f7621d Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 1 Nov 2024 11:22:08 -0300 Subject: [PATCH 61/63] man --- NAMESPACE | 2 + man/direction_group.Rd | 7 ++- man/direction_polarization.Rd | 7 ++- man/direction_step.Rd | 3 +- man/direction_to_centroid.Rd | 6 +- man/direction_to_leader.Rd | 107 ++++++++++++++++++++++++++++++++++ man/distance_to_centroid.Rd | 9 ++- man/distance_to_leader.Rd | 106 +++++++++++++++++++++++++++++++++ man/leader_direction_group.Rd | 17 +++--- 9 files changed, 242 insertions(+), 22 deletions(-) create mode 100644 man/direction_to_leader.Rd create mode 100644 man/distance_to_leader.Rd diff --git a/NAMESPACE b/NAMESPACE index cb04785a..fad615cb 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,7 +9,9 @@ export(direction_group) export(direction_polarization) export(direction_step) export(direction_to_centroid) +export(direction_to_leader) export(distance_to_centroid) +export(distance_to_leader) export(dyad_id) export(edge_dist) export(edge_nn) diff --git a/man/direction_group.Rd b/man/direction_group.Rd index 9ee8f334..560d441b 100644 --- a/man/direction_group.Rd +++ b/man/direction_group.Rd @@ -7,8 +7,8 @@ direction_group(DT, direction = "direction", group = "group") } \arguments{ -\item{DT}{input data.table with distance column generated by -\code{distance_step} and group column generated with \code{group_pts}} +\item{DT}{input data.table with direction column generated by +\code{direction_step} and group column generated with \code{group_pts}} \item{direction}{character string of direction column name, default "direction"} @@ -87,6 +87,7 @@ See examples of using mean group direction: Other Direction functions: \code{\link{direction_polarization}()}, -\code{\link{direction_step}()} +\code{\link{direction_step}()}, +\code{\link{direction_to_leader}()} } \concept{Direction functions} diff --git a/man/direction_polarization.Rd b/man/direction_polarization.Rd index 51312ebb..4db6d66e 100644 --- a/man/direction_polarization.Rd +++ b/man/direction_polarization.Rd @@ -7,8 +7,8 @@ direction_polarization(DT, direction = "direction", group = "group") } \arguments{ -\item{DT}{input data.table with distance column generated by -\code{distance_step} and group column generated with \code{group_pts}} +\item{DT}{input data.table with direction column generated by +\code{direction_step} and group column generated with \code{group_pts}} \item{direction}{character string of direction column name, default "direction"} @@ -87,6 +87,7 @@ See examples of using polarization: Other Direction functions: \code{\link{direction_group}()}, -\code{\link{direction_step}()} +\code{\link{direction_step}()}, +\code{\link{direction_to_leader}()} } \concept{Direction functions} diff --git a/man/direction_step.Rd b/man/direction_step.Rd index 0ec38ee2..aaa64f30 100644 --- a/man/direction_step.Rd +++ b/man/direction_step.Rd @@ -100,6 +100,7 @@ direction_step( Other Direction functions: \code{\link{direction_group}()}, -\code{\link{direction_polarization}()} +\code{\link{direction_polarization}()}, +\code{\link{direction_to_leader}()} } \concept{Direction functions} diff --git a/man/direction_to_centroid.Rd b/man/direction_to_centroid.Rd index c459ea37..312d8b28 100644 --- a/man/direction_to_centroid.Rd +++ b/man/direction_to_centroid.Rd @@ -7,7 +7,8 @@ direction_to_centroid(DT = NULL, coords = NULL) } \arguments{ -\item{DT}{input data.table} +\item{DT}{input data.table with centroid columns generated by eg. +\code{centroid_group}} \item{coords}{character vector of X coordinate and Y coordinate column names. Note: the order is assumed X followed by Y column names.} @@ -75,6 +76,7 @@ See example of using direction to group centroid: \link{centroid_group}, \link{group_pts} Other Distance functions: -\code{\link{distance_to_centroid}()} +\code{\link{distance_to_centroid}()}, +\code{\link{distance_to_leader}()} } \concept{Distance functions} diff --git a/man/direction_to_leader.Rd b/man/direction_to_leader.Rd new file mode 100644 index 00000000..48c107c4 --- /dev/null +++ b/man/direction_to_leader.Rd @@ -0,0 +1,107 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/direction_to_leader.R +\name{direction_to_leader} +\alias{direction_to_leader} +\title{Direction to group leader} +\usage{ +direction_to_leader(DT = NULL, coords = NULL, group = "group") +} +\arguments{ +\item{DT}{input data.table with 'rank_position_group_direction' column +generated by \code{leader_direction_group} and group column generated by +\code{group_pts}} + +\item{coords}{character vector of X coordinate and Y coordinate column names. +Note: the order is assumed X followed by Y column names.} + +\item{group}{group column name, generated by \code{group_pts}, default +'group'} +} +\value{ +\code{direction_to_leader} returns the input \code{DT} appended with +a \code{direction_leader} column indicating the direction to the group leader. + +A message is returned when the \code{direction_leader} column is already exist in the input \code{DT} +because it will be overwritten. +} +\description{ +\code{direction_to_leader} calculates the direction to the leader of each +spatiotemporal group. The function accepts a \code{data.table} with +relocation data appended with a \code{rank_position_group_direction} column +indicating the ranked position along the group direction generated with +\code{leader_direction_group(return_rank = TRUE)}. Relocation data should be +in planar coordinates provided in two columns representing the X and Y +coordinates. +} +\details{ +The \code{DT} must be a \code{data.table}. If your data is a +\code{data.frame}, you can convert it by reference using +\code{\link[data.table:setDT]{data.table::setDT}} or by reassigning using +\code{\link[data.table:data.table]{data.table::data.table}}. + +This function expects a \code{rank_position_group_direction} column +generated with \code{leader_direction_group(return_rank = TRUE)}, +a \code{group} column generated with the +\code{group_pts} function. The \code{coords} and \code{group} arguments +expect the names of columns in \code{DT} which correspond to the X and Y +coordinates and group columns. +} +\examples{ +# Load data.table +library(data.table) +\dontshow{data.table::setDTthreads(1)} + +# Read example data +DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc")) + +# Cast the character column to POSIXct +DT[, datetime := as.POSIXct(datetime, tz = 'UTC')] + +# Temporal grouping +group_times(DT, datetime = 'datetime', threshold = '20 minutes') + +# Spatial grouping with timegroup +group_pts(DT, threshold = 50, id = 'ID', + coords = c('X', 'Y'), timegroup = 'timegroup') + +# Calculate direction at each step +direction_step( + DT = DT, + id = 'ID', + coords = c('X', 'Y'), + projection = 32736 +) + +# Calculate group centroid +centroid_group(DT, coords = c('X', 'Y')) + +# Calculate group direction +direction_group(DT) + +# Calculate leader in terms of position along group direction +leader_direction_group( + DT, + coords = c('X', 'Y'), + return_rank = TRUE +) + +# Calculate direction to leader +direction_to_leader(DT, coords = c('X', 'Y')) +} +\references{ +See examples of using direction to leader and position within group: +\itemize{ +\item \url{https://doi.org/10.1016/j.anbehav.2023.09.009} +\item \url{https://doi.org/10.1016/j.beproc.2013.10.007} +\item \url{https://doi.org/10.1371/journal.pone.0036567} +} +} +\seealso{ +\link{distance_to_leader}, \link{leader_direction_group}, \link{group_pts} + +Other Direction functions: +\code{\link{direction_group}()}, +\code{\link{direction_polarization}()}, +\code{\link{direction_step}()} +} +\concept{Direction functions} diff --git a/man/distance_to_centroid.Rd b/man/distance_to_centroid.Rd index f27b5862..6b0022ae 100644 --- a/man/distance_to_centroid.Rd +++ b/man/distance_to_centroid.Rd @@ -13,10 +13,8 @@ distance_to_centroid( ) } \arguments{ -\item{DT}{input data.table} - -\item{coords}{character vector of X coordinate and Y coordinate column names. -Note: the order is assumed X followed by Y column names.} +\item{DT}{input data.table with centroid columns generated by eg. +\code{centroid_group}} \item{group}{group column name, generated by \code{group_pts}, default 'group'} @@ -102,6 +100,7 @@ See examples of using distance to group centroid: \link{centroid_group}, \link{group_pts} Other Distance functions: -\code{\link{direction_to_centroid}()} +\code{\link{direction_to_centroid}()}, +\code{\link{distance_to_leader}()} } \concept{Distance functions} diff --git a/man/distance_to_leader.Rd b/man/distance_to_leader.Rd new file mode 100644 index 00000000..049e4e9d --- /dev/null +++ b/man/distance_to_leader.Rd @@ -0,0 +1,106 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/distance_to_leader.R +\name{distance_to_leader} +\alias{distance_to_leader} +\title{Distance to group leader} +\usage{ +distance_to_leader(DT = NULL, coords = NULL, group = "group") +} +\arguments{ +\item{DT}{input data.table with 'rank_position_group_direction' column +generated by \code{leader_direction_group} and group column generated by +\code{group_pts}} + +\item{coords}{character vector of X coordinate and Y coordinate column names. +Note: the order is assumed X followed by Y column names.} + +\item{group}{group column name, generated by \code{group_pts}, default +'group'} +} +\value{ +\code{distance_to_leader} returns the input \code{DT} appended with +a \code{distance_leader} column indicating the distance to the group leader. + +A message is returned when the \code{distance_leader} column is already exist in the input \code{DT} +because it will be overwritten. +} +\description{ +\code{distance_to_leader} calculates the distance to the leader of each +spatiotemporal group. The function accepts a \code{data.table} with +relocation data appended with a \code{rank_position_group_direction} column +indicating the ranked position along the group direction generated with +\code{leader_direction_group(return_rank = TRUE)}. Relocation data should be +in planar coordinates provided in two columns representing the X and Y +coordinates. +} +\details{ +The \code{DT} must be a \code{data.table}. If your data is a +\code{data.frame}, you can convert it by reference using +\code{\link[data.table:setDT]{data.table::setDT}} or by reassigning using +\code{\link[data.table:data.table]{data.table::data.table}}. + +This function expects a \code{rank_position_group_direction} column +generated with \code{leader_direction_group(return_rank = TRUE)}, +a \code{group} column generated with the +\code{group_pts} function. The \code{coords} and \code{group} arguments +expect the names of columns in \code{DT} which correspond to the X and Y +coordinates and group columns. +} +\examples{ +# Load data.table +library(data.table) +\dontshow{data.table::setDTthreads(1)} + +# Read example data +DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc")) + +# Cast the character column to POSIXct +DT[, datetime := as.POSIXct(datetime, tz = 'UTC')] + +# Temporal grouping +group_times(DT, datetime = 'datetime', threshold = '20 minutes') + +# Spatial grouping with timegroup +group_pts(DT, threshold = 50, id = 'ID', + coords = c('X', 'Y'), timegroup = 'timegroup') + +# Calculate direction at each step +direction_step( + DT = DT, + id = 'ID', + coords = c('X', 'Y'), + projection = 32736 +) + +# Calculate group centroid +centroid_group(DT, coords = c('X', 'Y')) + +# Calculate group direction +direction_group(DT) + +# Calculate leader in terms of position along group direction +leader_direction_group( + DT, + coords = c('X', 'Y'), + return_rank = TRUE +) + +# Calculate distance to leader +distance_to_leader(DT, coords = c('X', 'Y')) +} +\references{ +See examples of using distance to leader and position within group: +\itemize{ +\item \url{https://doi.org/10.1111/jfb.15315} +\item \url{https://doi.org/10.1098/rspb.2017.2629} +\item \url{https://doi.org/10.1016/j.anbehav.2023.09.009} +} +} +\seealso{ +\link{direction_to_leader}, \link{leader_direction_group}, \link{group_pts} + +Other Distance functions: +\code{\link{direction_to_centroid}()}, +\code{\link{distance_to_centroid}()} +} +\concept{Distance functions} diff --git a/man/leader_direction_group.Rd b/man/leader_direction_group.Rd index b3835cbf..34e8708d 100644 --- a/man/leader_direction_group.Rd +++ b/man/leader_direction_group.Rd @@ -8,14 +8,15 @@ leader_direction_group( DT = NULL, group_direction = "group_direction", coords = NULL, - group = NULL, + group = "group", return_rank = FALSE, ties.method = "average" ) } \arguments{ -\item{DT}{input data.table with distance column generated by -\code{distance_step} and group column generated with \code{group_pts}} +\item{DT}{input data.table with group direction columns generated by +\code{direction_group} and centroid columns generated by +\code{centroid_group}} \item{group_direction}{group_direction column name generated using \code{direction_group}, default 'group_direction'} @@ -23,7 +24,8 @@ leader_direction_group( \item{coords}{character vector of X coordinate and Y coordinate column names. Note: the order is assumed X followed by Y column names.} -\item{group}{character string of group column name, default "group"} +\item{group}{group column name, generated by \code{group_pts}, default +'group'} \item{return_rank}{boolean if rank distance should also be returned, default FALSE} @@ -33,10 +35,9 @@ FALSE} \value{ \code{leader_direction_group} returns the input \code{DT} appended with a \code{position_group_direction} column indicating the position along -the group direction in the units of the projection and, optionally, a -\code{rank_position_group_direction} column indicating the -within group rank position along the group dirtection \code{return_rank = - TRUE}). +the group direction in the units of the projection and, optionally when +\code{return_rank = TRUE}, a \code{rank_position_group_direction} column +indicating the the ranked position along the group direction. A message is returned when \code{position_group_direction} or \code{rank_position_group_direction} columns already exist in the input From 457e8e2f2082014967949f3996f322bbb95ac875 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 1 Nov 2024 11:24:25 -0300 Subject: [PATCH 62/63] fix missing coords param --- R/distance_to_centroid.R | 1 + man/distance_to_centroid.Rd | 3 +++ 2 files changed, 4 insertions(+) diff --git a/R/distance_to_centroid.R b/R/distance_to_centroid.R index a93c6ca1..016dbda1 100644 --- a/R/distance_to_centroid.R +++ b/R/distance_to_centroid.R @@ -29,6 +29,7 @@ #' @param return_rank boolean if rank distance should also be returned, default #' FALSE #' @param ties.method see \code{\link[data.table:frank]{?data.table::frank}} +#' @inheritParams group_pts #' #' @return \code{distance_to_centroid} returns the input \code{DT} appended with #' a \code{distance_centroid} column indicating the distance to group centroid diff --git a/man/distance_to_centroid.Rd b/man/distance_to_centroid.Rd index 6b0022ae..2f471090 100644 --- a/man/distance_to_centroid.Rd +++ b/man/distance_to_centroid.Rd @@ -16,6 +16,9 @@ distance_to_centroid( \item{DT}{input data.table with centroid columns generated by eg. \code{centroid_group}} +\item{coords}{character vector of X coordinate and Y coordinate column names. +Note: the order is assumed X followed by Y column names.} + \item{group}{group column name, generated by \code{group_pts}, default 'group'} From 92ca9c6d28015f7bcf1076d17e053022da99cd16 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Fri, 1 Nov 2024 11:34:08 -0300 Subject: [PATCH 63/63] fix reduce example runtime --- R/direction_to_leader.R | 3 +++ R/distance_to_leader.R | 3 +++ R/leader_direction_group.R | 3 +++ man/direction_to_leader.Rd | 3 +++ man/distance_to_leader.Rd | 3 +++ man/leader_direction_group.Rd | 3 +++ 6 files changed, 18 insertions(+) diff --git a/R/direction_to_leader.R b/R/direction_to_leader.R index a9e1e89b..37431ff0 100644 --- a/R/direction_to_leader.R +++ b/R/direction_to_leader.R @@ -46,6 +46,9 @@ #' # Read example data #' DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc")) #' +#' # (Subset example data to reduce example run time) +#' DT <- DT[year(datetime) == 2016] +#' #' # Cast the character column to POSIXct #' DT[, datetime := as.POSIXct(datetime, tz = 'UTC')] #' diff --git a/R/distance_to_leader.R b/R/distance_to_leader.R index 78795526..6bbab8dd 100644 --- a/R/distance_to_leader.R +++ b/R/distance_to_leader.R @@ -52,6 +52,9 @@ #' # Cast the character column to POSIXct #' DT[, datetime := as.POSIXct(datetime, tz = 'UTC')] #' +#' # (Subset example data to reduce example run time) +#' DT <- DT[year(datetime) == 2016] +#' #' # Temporal grouping #' group_times(DT, datetime = 'datetime', threshold = '20 minutes') #' diff --git a/R/leader_direction_group.R b/R/leader_direction_group.R index 2a4dba9b..074df0db 100644 --- a/R/leader_direction_group.R +++ b/R/leader_direction_group.R @@ -65,6 +65,9 @@ #' # Read example data #' DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc")) #' +#' # (Subset example data to reduce example run time) +#' DT <- DT[year(datetime) == 2016] +#' #' # Cast the character column to POSIXct #' DT[, datetime := as.POSIXct(datetime, tz = 'UTC')] #' diff --git a/man/direction_to_leader.Rd b/man/direction_to_leader.Rd index 48c107c4..b200361f 100644 --- a/man/direction_to_leader.Rd +++ b/man/direction_to_leader.Rd @@ -54,6 +54,9 @@ library(data.table) # Read example data DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc")) +# (Subset example data to reduce example run time) +DT <- DT[year(datetime) == 2016] + # Cast the character column to POSIXct DT[, datetime := as.POSIXct(datetime, tz = 'UTC')] diff --git a/man/distance_to_leader.Rd b/man/distance_to_leader.Rd index 049e4e9d..ce7d5b05 100644 --- a/man/distance_to_leader.Rd +++ b/man/distance_to_leader.Rd @@ -57,6 +57,9 @@ DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc")) # Cast the character column to POSIXct DT[, datetime := as.POSIXct(datetime, tz = 'UTC')] +# (Subset example data to reduce example run time) +DT <- DT[year(datetime) == 2016] + # Temporal grouping group_times(DT, datetime = 'datetime', threshold = '20 minutes') diff --git a/man/leader_direction_group.Rd b/man/leader_direction_group.Rd index 34e8708d..e9ee6432 100644 --- a/man/leader_direction_group.Rd +++ b/man/leader_direction_group.Rd @@ -81,6 +81,9 @@ library(data.table) # Read example data DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc")) +# (Subset example data to reduce example run time) +DT <- DT[year(datetime) == 2016] + # Cast the character column to POSIXct DT[, datetime := as.POSIXct(datetime, tz = 'UTC')]