Given the following, from 1 USD to
- euro
- peso
- franc
- Australian dollar
- New Zealand dollar
- Canadian dollar
How would I write a function to convert from one currency to another?
The function should work like this - amount
is numeric, from
and to
are character strings:
currency(amount = 1, from = 'usd', to = 'euro')
## [1] 8.7
The only thing I can think if is to write numerous if statements, but that seems way too tedious for all these possibly currency conversions/combinations.
I'm also thinking of creating a named vector in my function like so: c('euro' = 0.93, 'peso' = 24.71, 'franc' = 0.98, ...)
and so on to show the conversion rates from 1 USD to these currencies. But still not sure how to write a function that accounts for all these currency conversions (USD, euro, peso, franc, Austrian dollar, New Zealand dollar, Canadian dollar).