Hi Roger,

Here's a little program that might help you out.  I'm using RCurl to
download the data, but the parsing code should work fine with the "doc" you
downloaded above:

#Load Libraries
library(RCurl)
library(XML)
library(xts)

#Settings
API_KEY <- 'XXXX'
fred.url <- "http://api.stlouisfed.org/fred/series/observations"

#Download Data
doc <- getForm(fred.url,
               .params =
                 list(
                   series_id="PSAVERT",
                   file_type="xml",
                   output_type=2,
                   observation_start="2012-03-28",
                   observation_end="2012-06-28",
                   vintage_date="1960-09-28",
                   api_key=API_KEY
                 )
)

#Parse Data
out <- xmlToList(xmlParse(doc))
attrs <- out$.attrs
out$.attrs <- NULL
out <- data.frame(do.call(rbind, out))

#Convert to XTS
out$PSAVERT_20120803 <- as.numeric(out$PSAVERT_20120803)
out$date <- as.Date(out$date)
out <- xts(out$PSAVERT_20120803, order.by=out$date)
out


-Zach

On Fri, Aug 3, 2012 at 10:16 AM, Bos, Roger <roger.bos@rothschild.com>wrote:

> All,
>
> I know that quantmod has a function getSymbols to download data from the
> Fed's FRED database, but the Fed also has a second version of the FRED
> database called ALFRED that allows you to get data by vintage date (before
> revisions).  Is there a function to access that database?  I tried to
> modify the function but couldn't figure out the comparable URL for the
> ALFRED databse.
>
> So I took a look at their API offering (
> http://api.stlouisfed.org/docs/fred/ ).  Its easy to use, but I am having
> trouble getting the resulting XML data into a data frame I can actually
> work with.  The code below works, but I am hesitant to give out my key, so
> I have XXXX'ed it out.  You can get an API key for free in less than 60
> seconds.
>
> library(XML)
> fred.url <- "
> http://api.stlouisfed.org/fred/series/observations?series_id=PSAVERT&file_type=xml&output_type=2&observation_start=2012-03-28&observation_end=2012-06-28&vintage_date=1960-09-28&api_key=XXXX
> "
> doc <- xmlParse(fred.url)
> doc
> xmlToDataFrame(doc)
> out <- xmlToList(doc)
> unlist(out)
>
> Even without running the code, someone might be able to help me out.  The
> XML examples work so well, but it seems the XML output from the AFLRED site
> is slightly different.   Here is what it looks like:
>
> >  doc <- xmlParse(fred.url)
>
> >  doc
> <?xml version="1.0" encoding="utf-8"?>
> <observations realtime_start="2012-08-03" realtime_end="2012-08-03"
> observation_start="2012-03-28" observation_end="2012-06-28" units="lin"
> output_type="2" file_type="xml" order_by="observation_date"
> sort_order="asc" count="3" offset="0" limit="100000">
>   <observation date="2012-04-01" PSAVERT_20120803="3.6"/>
>   <observation date="2012-05-01" PSAVERT_20120803="4.0"/>
>   <observation date="2012-06-01" PSAVERT_20120803="4.4"/>
> </observations>
>
> That looks very reasonable to me, but when I try to convert it to a data
> frame, I get an error that 'data' must be of a vector type:
>
> > xmlToDataFrame(doc)
> Error in matrix(vals, length(nfields), byrow = TRUE) :
>   'data' must be of a vector type
> >
>
> xmlToList almost gets me there, and I could probably write some code to
> extract the data and put it into a data frame, but there is probably a much
> easier way and I am just missing something because this is my first time
> parsing XML.
>
> > out <- xmlToList(doc)
>
> >  out
> $observation
>             date PSAVERT_20120803
>     "2012-04-01"            "3.6"
>
> $observation
>             date PSAVERT_20120803
>     "2012-05-01"            "4.0"
>
> $observation
>             date PSAVERT_20120803
>     "2012-06-01"            "4.4"
>
> $.attrs
>     realtime_start       realtime_end  observation_start
>  observation_end              units        output_type
>       "2012-08-03"       "2012-08-03"       "2012-03-28"
> "2012-06-28"              "lin"                "2"
>          file_type           order_by         sort_order
>  count             offset              limit
>              "xml" "observation_date"              "asc"
>  "3"                "0"           "100000"
>
>
> I am open to any advice anyone has.
>
> Thanks,
>
> Roger
>
>
>
> Roger J. Bos, CFA
> Rothschild Asset Management, Inc.
> Tel     +1 (212) 405-5471
> Email roger.bos@rothschild.com<mailto:roger.bos@rothschild.com>
> 1251 Avenue of the Americas, NY, NY  10020
>
>
> ***************************************************************
>
> This message is for the named person's use only. It may\...{{dropped:15}}
>
> _______________________________________________
> R-SIG-Finance@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
>

	[[alternative HTML version deleted]]

