R/download-billyields.R

#' Download treasury bills yields from Bank of Canada website.
#'
#' @param years_span          A positive integer
#'
#' @return tbillyields_bofc   Data.frame of treasury bills yields for the last \code{years_span} years
#'
#' @examples
#' # For the last year of data
#' download_billyields(1)
#'
#' # For the last 10 years of data
#' download_billyields(10)
#'
download_billyields <- function( years_span = 10 ){
  # Get url query string to grab data
  query_str <- create_tbills_query_string( years_span = years_span )

  # Download dataset
  temp <- tempfile( )
  download.file( query_str, temp, mode = "wb", quiet = TRUE )

  # Read in dataset and clean up
  tbillyields_bofc <- read.csv( temp, header = FALSE, stringsAsFactors = FALSE, skip = 19 )
  # create column names
  new_colnames <- c( "dates", "MONTH1", "MONTH3", "MONTH6", "MONTH12" )
  names( tbillyields_bofc ) <- new_colnames

  # Update dataset and return it
  unlink( temp )
  return( tbillyields_bofc )
}
vathymut/bondyields documentation built on May 3, 2019, 4:35 p.m.