R/eq_location_clean.R

Defines functions eq_location_clean

Documented in eq_location_clean

#'  Add a LOCATION column
#'
#' This function takes a NOAA data frame and adds a LOCATION column from LOCATION_NAME
#'
#' @param Data A raw NOAA data frame
#'
#' @return eq_location_clean returns a data frame with a LOCATION column.
#'
#' @importFrom dplyr mutate
#' @importFrom magrittr "%>%"
#' @importFrom stringr str_split
#' @importFrom stringr str_to_title
#'
#' @examples
#' \dontrun{
#' read_delim(system.file("extdata", "signif.txt", package = "TheCapstoneProject"), delim = '\t') %>%
#' eq_location_clean()
#' }
#'
#' @export
eq_location_clean <- function(Data)
{
  Data$LOCATION <- vector(length = nrow(Data))
  Data$LOCATION <- sapply(stringr::str_split(Data$LOCATION_NAME, ':', ), '[[', 1)
  Data$LOCATION[sapply(stringr::str_split(Data$LOCATION_NAME, ':'), 'length')>1] <- trimws(sapply(stringr::str_split(Data$LOCATION_NAME[sapply(stringr::str_split(Data$LOCATION_NAME, ':'), 'length')>1], ':', ), '[[', 2))
  Data$LOCATION <- stringr::str_to_title(Data$LOCATION)
  Data
}
ekawabata/TheCapstoneProject documentation built on June 27, 2020, 7:58 a.m.