Open matiasandina opened 1 year ago
Addressing the grouping issue
get_ethogram <- function(data, x, behaviour, sampling_period = NULL){
if (is.null(sampling_period)){
cli::cli_alert_warning("`sampling_period` not provided.")
sampling_period <- min(diff(dplyr::pull(data, {{x}})))
cli::cli_inform("Sampling period estimated to {sampling_period} using min difference between observations")
}
if(dplyr::is_grouped_df(data)){
cli::cli_alert_info("Data was grouped by {dplyr:::group_vars(data)}")
data <- dplyr::select(data, dplyr::group_cols(), x = {{x}}, behaviour = {{behaviour}})
} else {
data <- dplyr::select(data, x = {{x}}, behaviour = {{behaviour}})
}
etho <- data %>%
dplyr::mutate(run_id = vctrs::vec_identify_runs(behaviour)) %>%
# add to whatever previous layer was there
group_by(run_id, .add=TRUE) %>%
dplyr::summarise(behaviour = base::unique(behaviour),
xend = dplyr::last(x) + sampling_period,
x = dplyr::first(x),
duration = xend - x,
.groups = "keep") %>%
dplyr::select(dplyr::group_cols(), x, xend, behaviour, duration)
return(etho)
}
For example
Would return something like
The user needs to be cautious with the grouping of the
data.frame
and how they call each function. It would be great to handle thehas_x
+has_no_x
+ ... and everything we do for the plot itself inside one function, but maybe a few functions can simplify things and then have a wrapper ?