R/filter_last_loc.R

Defines functions filter_last_loc

Documented in filter_last_loc

#' Select the last location for each individual
#'
#' @param x A data frame containing collar data.
#' @param ... unquoted column name(s) to be used as a grouping variable e.g. if "id" is used, a single row will be returned for each id.
#' @param order_by A character vector of the column(s) to be used to arrange the data e.g. if the date/time column is used rows will be ordered chronologically.
#'
#' @return A data frame with the last location for each individual.
#' @export
#'
#' @examples
#' \dontrun{
#'   filter_last_loc(collar_data, ... = id, order_by = dt)
#' }
filter_last_loc <- function(x,
                            ...,
                            order_by

) {
  if(!missing(order_by)) x <- dplyr::arrange(x, {{ order_by }})

  x %>%
    dplyr::group_by( ... ) %>%
    dplyr::slice(dplyr::n()) %>%
    dplyr::ungroup()
}
Huh/CollaR documentation built on Aug. 8, 2022, 9:02 p.m.