I am in very unfamiliar terrain with downloading this file set from the Federal Reserve's website (I was hoping for an API but it doesn't appear to exist).
I am not sure what this Fed link format really is and how I would read the data into R.
I tried solutions here and here but I get different errors and not sure where to go. When I try something like this solution:
library(XML)
tf <- tempfile(tmpdir = tdir <- tempdir())
## Download the zip file
download.file("https://www.federalreserve.gov/datadownload/Output.aspx?rel=Z1&filetype=zip", tf)
## Unzip it in the temp folder
xml_files <- unzip(tf, exdir = tdir)
## Parse the first file
doc <- xmlInternalTreeParse(xml_files[1])
I get Warning message: In unzip(tf, exdir = tdir) : zip file is corrupt
So basically, I would like to be able to get the data without having to first go to the website, download the excel version, and then read the excel version into R.
I am assuming there is a way to just reference this link https://www.federalreserve.gov/datadownload/Output.aspx?rel=Z1&filetype=zip to read the data into R without the manual steps associated with first downloading in excel.
The link for different download options for this data is here: https://www.federalreserve.gov/datadownload/Choose.aspx?rel=Z1
Thanks!
Interestingly, I did not encounter the
unzipwarning on Linux but did on Windows and one notable difference of operating systems is character encoding.Therefore, consider the non-default binary mode (i.e.,
mode = "wb") ofdownload.filewhich does not raise a warning on Windows. This also makes sense for XML files which usually carry non-ASCII encoding such asUTF-8.In review, the Z1_data XML looks to be fairly flat and attribute-centric as shown in the head of the document with data observations nested under each series:
Therefore, consider using the undocumented,
xmlAttrsToDataFrame, and assign namespace prefixes for XPath parsing, to build data frames of all series and select observations: