R/set_period.R

Defines functions set_period

Documented in set_period

#' Set a resighting period
#'
#' @param x A dataframe generated by \code{format_bandedbirds} with columns for ResightDate and FlagID.
#' @param period The duration of one resighting period (time unit used to build encounter histories).
#'               Enter \code{"year"}, \code{"month"}, \code{"week"}, or \code{"other"}.
#' @param rsint If \code{period = "other"} enter the desired unit of time (i.e. \code{"3 days"}).
#' @return Dataframe with columns for ResightPeriod and FlagID (flag colour and
#'         code).
#' @examples
#' set_period(resight_data, period = "year")
#'
#' set_period(resight_data, period = "other", rsint = "3 days")

set_period <- function(x, period = NA, rsint = y) { # rsint stands for resight interval

  x$ResightDate <- mdy(x$ResightDate) # uses lubridate to make sure dates are formatted as dates
  if(period == "year") {

    resight_data <- x %>%
      mutate(ResightPeriod = year(ResightDate)) %>% # creates new column with year extracted from ResightDate
      select(ResightPeriod, FlagID) # removes Resight Date

  } else if
    (period == "month") {

      resight_data <- x %>%
        mutate(ResightPeriod = month(ResightDate)) %>% # creates new column with month extracted from ResightDate
        select(ResightPeriod, FlagID) # removes Resight Date

    } else if
      (period == "week") {
        resight_data <- x %>%
          mutate(ResightPeriod = week(ResightDate)) %>% # creates new column with week extracted from ResightDate
                                                        # week defaults to start on Sunday
          select(ResightPeriod, FlagID) # removes Resight Date

      } else
        resight_data <- x %>%
          mutate(ResightPeriod = lubridate::round_date(ResightDate, rsint)) %>% # creates new column that groups
          # dates into date intervals specified in rsint, start date is currently not customizable, function defaults it
          select(ResightPeriod, FlagID)


  assign("resight_data", resight_data, envir = globalenv())

}
ajmacdonald3/bb2enchist documentation built on Nov. 5, 2019, 4:47 p.m.