4
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)
GSee
  • 48,880
  • 13
  • 125
  • 145
jessica
  • 1,325
  • 2
  • 21
  • 35

2 Answers2

6

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.
GSee
  • 48,880
  • 13
  • 125
  • 145
5

FRED has a straightforward, well-document json interface http://api.stlouisfed.org/docs/fred/ which provides both metadata and time series data for all of its economic series. Access requires a FRED account and api key but these are available on request from http://api.stlouisfed.org/api_key.html .
The excel descriptive data you asked for can be retrieved using

get.FRSeriesTags <- function(seriesNam)
{
#     seriesNam = character string containing the ID identifying the FRED series to be retrieved    
#
library("httr")
library("jsonlite")
# dummy FRED api key; request valid key from http://api.stlouisfed.org/api_key.html
apiKey <- "&api_key=abcdefghijklmnopqrstuvwxyz123456"      
base  <- "http://api.stlouisfed.org/fred/"
seriesID <- paste("series_id=", seriesNam,sep="")
fileType <- "&file_type=json"
# 
# get series descriptive data
#
datType <- "series?"
url <- paste(base, datType, seriesID, apiKey, fileType, sep="")
series <- fromJSON(url)$seriess
# 
# get series tag data
#
datType <- "series/tags?"
url <- paste(base, datType, seriesID, apiKey, fileType, sep="")
tags <- fromJSON(url)$tags
#
# format as excel descriptive rows
#
description <- data.frame(Title=series$title[1], 
                      Series_ID = series$id[1], 
                      Source = tags$notes[tags$group_id=="src"][1],
                      Release = tags$notes[tags$group_id=="gen"][1],
                      Frequency = series$frequency[1],
                      Units = series$units[1],
                      Date_Range = paste(series[1, c("observation_start","observation_end")], collapse=" to "),
                      Last_Updated = series$last_updated[1],
                      Notes = series$notes[1],
                      row.names=series$id[1])
return(t(description))
}

Retrieving the actual time series data would be done in a similar way. There are several json packages available for R but jsonlite works particularly well for this application. There's a bit more to setting this up than the previous answer but perhaps worth it if you do much with FRED data.

WaltS
  • 5,410
  • 2
  • 18
  • 24