Created
November 17, 2009 13:54
-
-
Save drewconway/236910 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# File-Name: currency_converter.R | |
# Date: 2009-11-17 | |
# Author: Drew Conway | |
# Purpose: Convert currency data | |
# Data Used: vc_invests.csv | |
# Packages Used: foreign,XML | |
# Output File: vc_invests_USD.csv | |
# Data Output: | |
# Machine: Drew Conway's MacBook | |
library(foreign) | |
library(XML) | |
vc<-read.csv('vc_invests.csv') | |
currency.converter<-function(amount,from.currency,to.currency) { | |
# Converts amount (numeric) from given currency (character) to | |
# current USD equivalent. Currency characters must match three-letter | |
# currency codes (see here: http://www.xe.com/iso4217.php) | |
search.url<-paste("http://www.xe.com/ucc/convert.cgi?To=",to.currency,"&From=",from.currency,"&Amount=",as.integer(amount),sep="") | |
html<-capture.output(htmlTreeParse(search.url,isURL=TRUE)) | |
money.list<-unlist(strsplit(html[580],' ')) | |
return(as.numeric(gsub(",","",money.list[21]))) | |
} | |
for(c in 1:length(vc$currency)) { | |
size<-as.numeric(vc$round_size[c]) | |
cur<-vc$currency[c] | |
if(is.na(size)==FALSE & cur!="USD") { | |
vc$round_size[c]<-currency.converter(size,cur,"USD") | |
vc$currency[c]<-"USD" | |
} | |
} | |
write.csv(vc,"vc_invests_USD.csv") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment