R/utils.R

Defines functions numbers_site numeric_cleaner fix_columns

fix_columns <- function(data) {
  # Removes first 2 columns from numbers - that have empty column name
  data <- data[, names(data) != ""]
  # Removes the % change from last week to stay consistent in old column order.
  data$`%LW` <- NULL

  names(data) <- c("movie",
                   "distributor",
                   "gross",
                   "percent_change",
                   "theaters",
                   "per_theater",
                   "total_gross",
                   "days")

  # Fixes strange ... when runs out of space to ... that is readable to R.
  data$movie       <- iconv(data$movie, "latin1", "ASCII", sub = "")
  data$distributor <- iconv(data$distributor, "latin1", "ASCII", sub = "")

  return(data)
}

numeric_cleaner <- function(x) {
  # Removes everything but numbers and the negative sign and the period,
  # then makes numeric.
  suppressWarnings(as.numeric(gsub("[^[:digit:]\\.\\-]", "", x)))
}

numbers_site <- function(page){
  page <- rvest::html_nodes(page, paste0("#box_office_daily_table"))
  page <- rvest::html_table(page)
  page <- page[[1]]

  return(page)
}
jacobkap/boxoffice documentation built on July 27, 2020, 4:55 a.m.