R/nzffdr_add_dates.R

Defines functions nzffdr_add_dates

Documented in nzffdr_add_dates

#' Add dates to a NZFFD dataset
#'
#' Add year, month and day columns to a NZFFD dataset.
#'
#' Adds year, month and day columns to a NZFFD dataset, based on values in 
#' the "eventDate" column.
#' 
#' @param fishd a dataframe imported from the NZFFD using \code{nzffdr_import()}, which contains the column \code{"eventDate"}
#'
#' @return a NZFFD dataframe, with year, month and day columns added.
#'
#' @importFrom tidyr separate
#' @importFrom rlang .data
#'
#' @examples
#' nzffdr_add_dates(nzffdr::nzffdr_data)
#' 
#' @export
nzffdr_add_dates <- function(fishd){
  
  if (!any(colnames(fishd) == "eventDate")) {
    stop("fishd must be a data.frame returned from a call to nzffd_import(), containing the \"eventDate\" column")
  }
  
  # add year, month, day columns
  fishd <-  tidyr::separate(data = fishd,
                    col = .data$eventDate,
                    into = c("year", "month", "day"),
                    sep = "-",
                    remove = FALSE,
                    fill = "right",
                    convert = TRUE)

return(fishd)
}

Try the nzffdr package in your browser

Any scripts or data that you put into this service are public.

nzffdr documentation built on Nov. 10, 2022, 5:51 p.m.