[R] currency conversion function?

Liviu Andronic landronimirc at gmail.com
Wed Oct 14 18:45:50 CEST 2009


Hello

On 10/14/09, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
>  foo('BRL', 'USD', '2009-10-14')
>
Nice function, thank you. Two issues, though:
- it seems to provide reverse output. Example:
## how many dollars do you get "from" one euro?
> foo('EUR', 'USD', '2009-10-14')
[1] 0.67544
## however, the equivalent of 1 Euro would be ..
> 1/foo('EUR', 'USD', '2009-10-14')
[1] 1.4805
## .. dollars
## 1 Euro = 1.48051 US Dollar ## taken from the on-line converter

The dirty hack is to use "1/as.numeric(value)" as a return value. For
describing the next issue I will use the improved version of the
function.

- second issue, for weaker currencies (that is, with more digits) the
reported value is not necessarily correct. Example:
## fine
> foo('EUR', 'RUB', '2009-10-14')
[1] 43.745
## fine
> 1/foo('EUR', 'RUB', '2009-10-14')
[1] 0.02286
## wrong
> foo('RUB', 'EUR', '2009-10-14')
[1] 0.26878
## taken from the on-line converter
## 1 Euro = 43.75188 Russian Rouble
## 1 Russian Rouble (RUB) = 0.02286 Euro (EUR)

I am not sure on how to fix this one. Thank you
Liviu




More information about the R-help mailing list