R/table_funs.R

#' Given a sector number, return a description.
#'
#' @export
#' @param sector_number A Sector Number (1 - 206)
#' @param sectors A Vector of Sector Numbers (1 - 206).
#' @return The Industry.Commodity.Description for \code{sector_number} or \code{sectors}.
#' @examples
#' sector_description(27)
#' sector_description(c(27, 28))
sector_description <- function(sector_number, sectors) {
  return(sectors[sector_number, 'Industry.Commodity.Description'])
}


#' Return the employment requirements table for the given year, current by default
#'
#' @export
#' @param year A table year, current is default.
#' @param tablelist An employment matrix requirements table.
#' @return The table for \code{year} from \code{tabelist}.
#' @examples
#' table_by_year()
#' table_by_year(1997, dom_table_list)
  table_by_year <- function(year = LATEST_YEAR, tablelist = dom_table_list) {
  year_list <- tablelist[[as.character(year)]]
}

#' Return the latest employment requirements table, convenience wrapper for table_year defaults
#'
#' @export
#' @return The current table for \code{tablelist}.
#' @examples
#' current_table()
#' current_table(tablelist = all_table_list)
current_table <- function(tablelist = dom_table_list) {
  table_by_year(year = LATEST_YEAR, tablelist = tablelist)
}

#' Return the latest imports employment requirements table, using the convenience wrapper for table_year defaults
#'
#' @export
#' @return The current imports table.
#' @examples
#' imports_table_current()
imports_table_current <- function() {
  table_by_year(tablelist = imports_table_list)
}

#' Return the employment requirements coefficient vector for a sector, the number of jobs
#' generated in all sectors, direct and indirect, by one million dollars output of any
#' sector-column
#'
#' @export
#' @param series_table A \code{series_table} for the domestic, imports, or total employment tables.
#' @param sector_number A \code{sector number}.
#' @return The series table for \code{sector_number}.
#' @examples
#' series_by_sector(a_series_table, 27)
series_by_sector <- function(series_table, sector_number) {
  as.vector(series_table[ , sector_number + 1])  # Get the column for that sector, note first column is description
}

#' Return the employment requirements coefficient vector for a sector and given year, latest year by default
#'
#' @export
#' @param sector_number A \code{sector_number}.
#' @param year The table \code{year}.
#' @param tablelist A \code{tablelist} for the domestic, imports, or total employment tables list.
#' @return The series table for \code{sector_number}, \code{year} and \code{tablelist}.
#' @examples
#' series_by_sector_by_year(27, 1997, all_table_list)
series_by_sector_by_year <- function(sector_number, year = LATEST_YEAR, tablelist = dom_table_list) {
  series_by_sector(table_by_year(year, tablelist), sector_number)
}

#' Return a vector of the number of jobs by all sectors for a given amount of production of a sector
#'
#' @export
#' @param sector_number A \code{sector_number}.
#' @param prod_output_mm  \code{prod_output_mm}, the amount of final output in $1 million USD.
#' @param year The table \code{year}.
#' @param tablelist A \code{tablelist} for the domestic, imports, or total employment tables list.
#' @return The number of employed for \code{prod_output_mm} dollars production of \code{sector_number} output, \code{year} and \code{tablelist}.
#' @examples
#' sector_emp_impact(27, 10.5, 1997, all_table_list)
sector_emp_impact <- function(sector_number, prod_output_mm, year = LATEST_YEAR, tablelist = dom_table_list) {
  series_by_sector_by_year(sector_number, year, tablelist) * prod_output_mm
}

#' Returns the total number of jobs (domestic only) in all sectors that is generated by
#'  $1 million of production of that sector (in 2009 dollars)
#'
#' @export
#' @param sector_number A \code{sector_number}.
#' @param prod_output_mm  \code{prod_output_mm}, the amount of final output in $1 million USD.
#' @param year The table \code{year}.
#' @param tablelist A \code{tablelist} for the domestic, imports, or total employment tables list.
#' @return The total number of direct and indirect employed for \code{prod_output_mm} dollars production of \code{sector_number} output, \code{year} and \code{tablelist}.
#' @examples
#' emp_impact_all_sectors(27, 10.5, 1997, all_table_list)
emp_impact_all_sectors <- function(sector_number, prod_output_mm, year = LATEST_YEAR, tablelist = dom_table_list) {
  sum(series_by_sector_by_year(sector_number, year, tablelist)) * prod_output_mm
}

#' Returns the number of jobs (default: domestic only, current year) directly employed in a sector generated by
#'  $1 million of production of that sector (in 2009 dollars)
#'
#' @export
#' @param sector_number A \code{sector_number}.
#' @param prod_output_mm  \code{prod_output_mm}, the amount of final output in $1 million USD.
#' @param year The table \code{year}.
#' @param tablelist A \code{tablelist} for the domestic, imports, or total employment tables list.
#' @return The total number of direct employed for \code{prod_output_mm} dollars production of \code{sector_number} output, \code{year} and \code{tablelist}.
#' @examples
#' direct_emp_impact(27, 10.5, 1997, all_table_list)
direct_emp_impact <- function(sector_number, prod_output_mm, year = LATEST_YEAR, tablelist = dom_table_list) {
  table_by_year(year, tablelist)[sector_number, sector_number + 1] * prod_output_mm
}

#' Returns the number of jobs (default: domestic only, current year) indirectly employed in a sector generated by
#'  $1 million of production of that sector (in 2009 dollars)
#'
#' @export
#' @param sector_number A \code{sector_number}.
#' @param prod_output_mm  \code{prod_output_mm}, the amount of final output in $1 million USD.
#' @param year The table \code{year}.
#' @param tablelist A \code{tablelist} for the domestic, imports, or total employment tables list.
#' @return The total number of indirectly employed for \code{prod_output_mm} dollars production of \code{sector_number} output, \code{year} and \code{tablelist}.
#' @examples
#' indirect_emp_impact(27, 10.5, 1997, all_table_list)
indirect_emp_impact <- function(sector_number, prod_output_mm, year = LATEST_YEAR, tablelist = dom_table_list) {
                                emp_impact_all_sectors(sector_number,
                                prod_output_mm, year, tablelist) - direct_emp_impact(sector_number, prod_output_mm, year, tablelist)
}

#' Returns a vector of the number of jobs (default: domestic only) directly and indirectly employed in a sector generated by
#'  $1 million of production of that sector (in 2009 dollars) for all years
#'
#' @export
#' @param sector_number A \code{sector_number}.
#' @param prod_output_mm  \code{prod_output_mm}, the amount of final output in $1 million USD.
#' @param tablelist A \code{tablelist} for the domestic, imports, or total employment tables list.
#' @return The total number of indirectly employed for \code{prod_output_mm} dollars production of \code{sector_number} output, \code{year} and \code{tablelist}.
#' @examples
#' sector_all_impact_all_years(27, 10.5, all_table_list)
sector_all_impact_all_years <- function(sector, prod_output, tablelist = dom_table_list) {
  sapply(TABLE_YEARS, emp_impact_all_sectors, sector_number = sector,
         prod_output_mm = prod_output, tablelist)
}

#' Returns a vector of the number of jobs (default: domestic only) directly employed in a sector generated by
#'  $1 million of production of that sector (in 2009 dollars) for all years
#'
#' @export
#' @param sector_number A \code{sector_number}.
#' @param prod_output_mm  \code{prod_output_mm}, the amount of final output in $1 million USD.
#' @param tablelist A \code{tablelist} for the domestic, imports, or total employment tables list.
#' @return The total number of directly employed for \code{prod_output_mm} dollars production of \code{sector_number} output, \code{year} and \code{tablelist}.
#' @examples
#' sector_direct_impact_all_years(27, 10.5, all_table_list)
sector_direct_impact_all_years <- function(sector, prod_output, tablelist = dom_table_list) {
  sapply(TABLE_YEARS, direct_emp_impact, sector_number = sector,
         prod_output_mm = prod_output, tablelist)
}

#' Returns a vector of the number of jobs (default: domestic only) directly employed in a sector generated by
#'  $1 million of production of that sector (in 2009 dollars) for all years
#'
#' @export
#' @param sector_number A \code{sector_number}.
#' @param prod_output_mm  \code{prod_output_mm}, the amount of final output in $1 million USD.
#' @return The total number of indirectly employed for \code{prod_output_mm} dollars production of \code{sector_number} output, \code{year} and \code{tablelist}.
#' @examples
#' sector_indirect_impact_all_years(27, 10.5)
sector_indirect_impact_all_years <- function(sector, prod_output, tablelist = dom_table_list) {
  sector_all_impact_all_years(sector, prod_output, tablelist = dom_table_list) -
  sector_direct_impact_all_years(sector, prod_output, tablelist = dom_table_list)
}
amilleranalytics/rempreq documentation built on May 10, 2019, 10:27 a.m.