Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quantmod FRED Metadata in R

Tags:

r

quantmod

library(quantmod)

getSymbols("GDPC1",src = "FRED")

I am trying to extract the numerical economic/financial data in FRED but also the metadata. I am trying to chart CPI and have the meta data as a labels/footnotes. Is there a way to extract this data using the quantmod package?

Title:               Real Gross Domestic Product
Series ID:           GDPC1
Source:              U.S. Department of Commerce: Bureau of Economic Analysis
Release:             Gross Domestic Product
Seasonal Adjustment: Seasonally Adjusted Annual Rate
Frequency:           Quarterly
Units:               Billions of Chained 2009 Dollars
Date Range:          1947-01-01 to 2014-01-01
Last Updated:        2014-06-25 7:51 AM CDT
Notes:               BEA Account Code: A191RX1

                     Real gross domestic product is the inflation adjusted value of the
                     goods and services produced by labor and property located in the
                     United States. 

                     For more information see the Guide to the National Income and Product
                     Accounts of the United States (NIPA) -
                     (http://www.bea.gov/national/pdf/nipaguid.pdf)
like image 419
jessica Avatar asked Dec 20 '22 12:12

jessica


1 Answers

You can use the same code that's in the body of getSymbools.FRED, but change ".csv" to ".xls", then read the metadata you're interested in from the .xls file.

library(gdata)

Symbol <- "GDPC1"
FRED.URL <- "http://research.stlouisfed.org/fred2/series"

tmp <- tempfile()
download.file(paste0(FRED.URL, "/", Symbol, "/downloaddata/", Symbol, ".xls"),
              destfile=tmp)
read.xls(tmp, nrows=17, header=FALSE)
#                      V1                                                                    V2
# 1                Title:                                           Real Gross Domestic Product
# 2            Series ID:                                                                 GDPC1
# 3               Source:              U.S. Department of Commerce: Bureau of Economic Analysis
# 4              Release:                                                Gross Domestic Product
# 5  Seasonal Adjustment:                                       Seasonally Adjusted Annual Rate
# 6            Frequency:                                                             Quarterly
# 7                Units:                                      Billions of Chained 2009 Dollars
# 8           Date Range:                                              1947-01-01 to 2014-01-01
# 9         Last Updated:                                                2014-06-25 7:51 AM CDT
# 10               Notes:                                             BEA Account Code: A191RX1
# 11                         Real gross domestic product is the inflation adjusted value of the
# 12                           goods and services produced by labor and property located in the
# 13                                                                            United States. 
# 14                                                                                           
# 15                      For more information see the Guide to the National Income and Product
# 16                                                     Accounts of the United States (NIPA) -
# 17                                             (http://www.bea.gov/national/pdf/nipaguid.pdf)

Instead of hardcoding nrows=17, you can use grep to search for the row that has the headers of the data, and subset to only include rows before that.

dat <- read.xls(tmp, header=FALSE, stringsAsFactors=FALSE)
dat[seq_len(grep("DATE", dat[, 1])-1),]

unlink(tmp)  # remove the temp file when you're done with it.
like image 163
GSee Avatar answered Jan 09 '23 02:01

GSee