我有一个数据框,其中包含不同日期的不同货币金额,并且想要创建一个新列,显示在该日期它等于的金额。
例如我的数据可能如下
data <- data.frame(
date = seq.Date(from = as.Date('2017-01-01'), to = as.Date('2017-01-10'), length = 10),
currency = c('EUR', 'GBP', 'USD', 'HKD', 'GBP', 'EUR', 'CAN', 'AUD', 'EUR', 'GBP'),
amount = 1:10
)
但是我想要一个通用函数来执行此操作,无论日期范围和数据框架中的货币如何都可以使用。
我很确定我可以使用getFX
包中的quantmod
函数执行此操作,但无法完成最佳方法!
我确信此操作已经完成,所以我们将不胜感激。
由于
答案 0 :(得分:2)
使用功能?merge
,?lapply
并按照以下步骤操作:
<强>步骤:强>
1)创建货币对EUR / GBP,USD / GBP
2)获取外汇数据
3)重塑/修改外汇数据
4)与原始数据合并并计算fx翻译量
library("quantmod")
library("reshape2") #for melt function
#replaced CAN by CAD for Canadian Dollar
currDF <- data.frame(
date = seq.Date(from = as.Date('2017-01-01'), to = as.Date('2017-01-10'), length = 10),
currency = c('EUR', 'GBP', 'USD', 'HKD', 'GBP', 'EUR', 'CAD', 'AUD', 'EUR', 'GBP'),
amount = 1:10,stringsAsFactors=FALSE)
currDF
外汇数据:
#Set start and end dates
startDt = as.Date("2017-01-01")
endDt = as.Date("2017-01-10")
#create currency pair combinations i.e. EUR/GBP, USDGBP
currCombinations = paste(setdiff(unique(currDF$currency),"GBP"),"GBP",sep="/")
currCombinations
#[1] "EUR/GBP" "USD/GBP" "HKD/GBP" "CAD/GBP" "AUD/GBP"
#get FX data for each currency pair and merge them into a single xts dataset
#see ?lapply, ?do.call and ?merge.xts
#note auto.assign needs to be FALSE
fxData = do.call(merge.xts,lapply(currCombinations,function(x)
getFX(x,from=startDt,to=endDt,auto.assign=FALSE)))
fxData
# EUR.GBP USD.GBP HKD.GBP CAD.GBP AUD.GBP
#2017-01-01 0.852550 0.810242 0.104474 0.602773 0.583503
#2017-01-02 0.852550 0.810242 0.104474 0.602773 0.583503
#2017-01-03 0.850220 0.814326 0.104987 0.606361 0.587630
#2017-01-04 0.850310 0.815139 0.105102 0.609074 0.591082
#2017-01-05 0.853695 0.810205 0.104480 0.610352 0.591940
#2017-01-06 0.855625 0.808914 0.104311 0.610882 0.592583
#2017-01-07 0.857335 0.813948 0.104951 0.614860 0.593995
#2017-01-08 0.857330 0.813942 0.104951 0.614847 0.593990
#2017-01-09 0.864095 0.819833 0.105707 0.619112 0.600639
#2017-01-10 0.870830 0.822656 0.106077 0.622248 0.605533
#remove .GBP from all columns above
colnames(fxData) = gsub("[.]GBP","",colnames(fxData))
#set conversion factor for GBP = 1
fxData$GBP = 1
数据重塑:
#create data.frame from xts data
fxData_DF = data.frame(date=index(fxData),coredata(fxData),stringsAsFactors=FALSE)
#To make fx dataset(wide format) amenable for merging with currDF
#we convert it to long format by using melt from reshape2 package see,?melt
fxMolten = melt(fxData_DF,id="date",variable.name="currency",value.name="conversionFactor")
head(fxMolten,2)
# date currency conversionFactor
#1 2017-01-01 EUR 0.85255
#2 2017-01-02 EUR 0.85255
tail(fxMolten,2)
# date currency conversionFactor
#59 2017-01-09 GBP 1
#60 2017-01-10 GBP 1
合并数据:
#For each currency and date, we need a conversion factor hence
#we merge both datasets by columns date and currency
fxMerged = merge(currDF,fxMolten,by=c("date","currency"))
#calculate FX translated amount
fxMerged$convAmount = fxMerged$amount * fxMerged$conversionFactor
fxMerged
# date currency amount conversionFactor convAmount
#1 2017-01-01 EUR 1 0.852550 0.852550
#2 2017-01-02 GBP 2 1.000000 2.000000
#3 2017-01-03 USD 3 0.814326 2.442978
#4 2017-01-04 HKD 4 0.105102 0.420408
#5 2017-01-05 GBP 5 1.000000 5.000000
#6 2017-01-06 EUR 6 0.855625 5.133750
#7 2017-01-07 CAD 7 0.614860 4.304020
#8 2017-01-08 AUD 8 0.593990 4.751920
#9 2017-01-09 EUR 9 0.864095 7.776855
#10 2017-01-10 GBP 10 1.000000 10.000000