R/cleaning-helpers.R

Defines functions outfile_path celsify_temp f_to_c localize_beach

#' @import dplyr
#' @import tidyverse
#'
#cat(stringi::stri_escape_unicode(x))
lookup_table <- dplyr::tribble(
  ~where, ~english,
  "beach",     "US",
  "coast",     "US",
  "seashore",     "UK",
  "seaside",     "UK"
)

#' @export
localize_beach <- function(dat) {
  dplyr::left_join(dat, lookup_table)
}

f_to_c <- function(x) (x - 32) * 5/9

#' @export
celsify_temp <- function(dat) {
  dplyr::mutate(dat, temp = dplyr::if_else(english == "US", f_to_c(temp), temp))
}

# now <- Sys.time()
# timestamp <- function(time) format(time, "%Y-%B-%d_%H-%M-%S")

#' @export
outfile_path <- function(infile, time = Sys.time()) {
  ts <- timestamp(time)
  paste0(ts, "_", sub("(.*)([.]csv$)", "\\1_clean\\2", infile))
}

utils::globalVariables(c("english", "temp"))
namrouche993/newfolder documentation built on Jan. 29, 2022, 3:01 a.m.