forked from agitea/invacost_FUN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrency_exchange.R
More file actions
110 lines (77 loc) · 4.53 KB
/
currency_exchange.R
File metadata and controls
110 lines (77 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
## Script to convert cost in local currency to USD
## written by Anna J. Turbelin, November 14, 2022
## aturbelin@gmail.com
library(knitr)
library(dplyr)
library(ggplot2)
library(devtools)
library(mgcv)
library(ggpubr)
library(readxl)
library(tidyverse)
library(ggpmisc)
install.packages("wbstats")
library(wbstats)
library(readr)
# library(OECD)
# library(countrycode)
# library(ISOcodes)
options(stringsAsFactors=FALSE)
## function 2.0 to adjust costs for inflation with option to have a 'neutral' fixed year' original CPI column
##' @param costdb Name of database to pull costs from that need to be converted.
##' @param costcolumn Name of the cost column to use in \code{costdb}. Original currency raw costs to be converted.
##' @param yearcolumn Name of the year column to use. This is the column that records the year when the costs were reported or when the currency is applicable.
##' @param currencycolumn Name of the currency column to use. This is the column that records the currency in which original costs were reported.
##' @param originalyear Year of the current cost value - use when @param yearcolumn is null because costs have already been standardized to a given year.
##' @param method Name of the method to use to convert costs to USD - currently method used in "WB" which is just the data from worldbank
currencyXchange <- function(costdb, costcolumn, yearcolumn = "Applicable_year", currencycolumn = "Currency", originalyear = NULL, method = "WB"){
if(is.null(costcolumn))
{
stop("Add column name of raw costs in local currency to convert to USD")
}
if(!(costcolumn %in% colnames(costdb)))
{
stop("The 'costcolumn' does not exist in the database, please check spelling.")
}
if(!(yearcolumn %in% colnames(costdb)))
{
stop("The 'yearcolumn' does not exist in the database, please check spelling.")
}
if(!(currencycolumn %in% colnames(costdb)))
{
stop("The 'currencycolumn' does not exist in the database, please check spelling.")
}
Xrate.dat <- wb_data("PA.NUS.FCRF", country = "all") ## get exchange rate data from world bank
urlfile = "https://raw.githubusercontent.com/agitea/invacost_FUN/main/data/CurrencyName.csv"
currency.name <- read_csv(url(urlfile))
costUSD <- do.call(rbind,lapply(costdb$Cost_ID, function(x, costdb.,
cost,
year,
currency){
currency.code = costdb.[which(costdb.$Cost_ID == x), currency]
year.app = costdb.[which(costdb.$Cost_ID == x), year]
currency.country <- currency.name[which(currency.name$Currency_code == currency.code), colnames(currency.name)=="ISO"]
if(isTRUE(length(currency.country$ISO) == 0 & currency.code == "REA")){currency.country = "BRA"}else{
if(isTRUE(length(currency.country$ISO) == 0 & currency.code == "SOL")){currency.country = "PER"}else{
if(isTRUE(length(currency.country$ISO) == 0 & currency.code == "COL")){currency.country = "COL"}else{
if(length(currency.country$ISO)>1){
if(currency.code == "EUR"){currency.country = "EMU"} else {
if(currency.code == "USD"){currency.country = "USA"} else {
if(currency.code == "XPF"){currency.country = "NCL"} else {
currency.country <- currency.name[which(currency.name$Currency_code == currency.code & currency.name$country_type != "0" & currency.name$country_type != "Dependency"), colnames(currency.name)=="ISO"]
currency.country <-currency.country[1, colnames(currency.country)=="ISO"]
}
}
}
} else {currency.country <- currency.country[[1]]}
}}}
currency.country <- currency.country[[1]]
Xchange.rate = Xrate.dat[which(Xrate.dat$iso3c == currency.country & Xrate.dat$date == year.app), colnames(Xrate.dat)=="PA.NUS.FCRF"]
cost.raw.local = costdb.[which(costdb.$Cost_ID == x), cost]
cost.USD <- cost.raw.local/Xchange.rate
colnames(cost.USD) <- paste0("cost_USD")
return(cost.USD)
}, costdb. = costdb, cost = costcolumn, year = yearcolumn, currency = currencycolumn
))
return(df <- data.frame(dplyr::bind_cols(costdb, costUSD))) #this returns a new data frame - probably best to add to current dataset but not 100% how to do it
}