R/earqke_location_clean.R

Defines functions earqke_location_clean

#' earqke_location_clean
#'
#' This function is a intermediate step to clean the raw data. The `eq_clean_data` will use it behind
#' the scenes to insert the column LOCATION.
#'
#' @param df_2 A DataFrame with LOCATION_NAME column.
#'
#' @importFrom stringr str_split str_to_title
#'
#' @importFrom rlang .data
#'
#' @return Adds a new column to the DataFrame called LOCATION, following the specifications:
#'         Title Case and without country name.
#'
#' @examples
#'
#' \dontrun{
#' # Piping a DataFrame with LOCATION_NAME to be converted in LOCATION.
#' readr::read_delim("inst/extdata/signif.txt",
#'                   delim = "\t") %>%
#'                       earqke_location_clean()}
#'
#' @export
earqke_location_clean <- function(df_2 = rlang::.data) {

# Creating subset to work
test <- stringr::str_split(df_2$LOCATION_NAME, ";", simplify = TRUE)

# Temporary variable to store the cities names.
cities = vector()

# Loop to varies the columns
  for (col in 1:dim(teste)[2]) {

# Loop to varies the rows.
    for (row in 1:dim(test)[1]) {
      temp <- stringr::str_split(test[row,col], ":",
                                 simplify = TRUE)
      temp <- trimws(temp, which = 'left')  # Removing space on left.
      temp <- stringr::str_to_title(temp)            # Convering title case.
# Initialize the vector.
      if (col == 1) {
        cities[row] <- temp[length(temp)]     # Assigning values.
      }
# Adding new cities preserving the other city.
      else {
# Preventing to add "" and comas.
        if (temp == '')
          next
# Adding new cities after coma.
        else
          cities[row] <- paste(cities[row],
                               temp[length(temp)],
                               sep = ", ")
      }
    }
  }

# Adding new column with cities names in Title Case.
df_2['LOCATION'] <- cities

# Returning an updated DataFrame
return(df_2)
}
anamikapoyil04/NCEI-Earthquakes documentation built on Jan. 3, 2022, 4:35 a.m.