Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
gold prices from measuring worth in r
#R example of using prices from http://www.measuringworth.com
#if you use and like their data, I strongly encourage donating to the cause http://www.measuringworth.com/contribute.php
require(latticeExtra)
require(xts)
#ugly way to specify url for prices you would like
#shiny would provide a very nice interface once I get motivated
#this particular example will download prices from 1718 to 2013 for New York and London market
#redundant since London market dataset switched to New York in 1950
url = paste("http://www.measuringworth.com/datasets/gold/export.php?",
"year_source=1791&", #1257, 1786, 1791, 1687, or 1718
"year_result=",format(Sys.Date(),"%Y"),"&", #any year to current except for 1945 for British official
#set on for the following data sources
#time range set above must fall within the years specified
#can specify multiple sources
"British=&", #British official price (1257-1945)
"us=&", #U.S. official price (1786-Present)
"newyork=on&", #New York market price (1791-Present)
"goldsilver=&", #Gold/silver price ratio (1687-Present)
"london=on", #London market price (1718-Present) switches to New York 1950
sep="")
goldprices <- apply(read.csv(url,stringsAsFactors=FALSE),MARGIN=2,FUN=as.numeric)
#get in xts format for easier time-series handling
goldprices.xts <- na.omit(xts(goldprices[,2:ncol(goldprices)],
order.by = as.Date(paste(goldprices[,1],"-12-31",sep="")))) #set each year to end of year 12/31/Year
xyplot(goldprices.xts,
scales=list(x=list(tck=c(1,0)),y=list(log=10,rot=0)),
yscale.components = yscale.components.log10ticks,
xlab=NULL,
main="Gold Prices from MeasuringWorth (http://measuringworth.com)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment