How to get currency exchange rates in R

后端 未结 3 997
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 15:22

Are there are any R packages/functions to get exchange rates in real time, e.g. from Google Finance? Would prefer to avoid RCurl or other parsers if something\'s already out

3条回答
  •  囚心锁ツ
    2020-12-24 15:45

    You could use historical_exchange_rates() from the priceR library.

    E.g. to get the daily AUD to USD exchange rate from 2010 to 2020:

    # install.packages("priceR")
    library(priceR)
    
    cur <- historical_exchange_rates("AUD", to = "USD",
                              start_date = "2010-01-01", end_date = "2020-06-30")
    
    tail(cur)
    
           date one_AUD_equivalent_to_x_USD
     2020-06-25                    0.688899
     2020-06-26                    0.686340
     2020-06-27                    0.686340
     2020-06-28                    0.685910
     2020-06-29                    0.687335
     2020-06-30                    0.690166
    
    dim(cur)
    [1] 3834    2
    
    
    # Plot USD vs AUD last 10 years
    library(ggplot2)
    library(tidyverse)
    
    cur %>% 
      tail(365 * 10) %>% 
      rename(aud_to_usd = one_AUD_equivalent_to_x_USD) %>%  
      mutate(date = as.Date(date)) %>% 
      ggplot(aes(x = date, y = aud_to_usd, group = 1)) +
      geom_line() +
      geom_smooth(method = 'loess') + 
      theme(axis.title.x=element_blank(),
            axis.ticks.x=element_blank()) + 
      scale_x_date(date_labels = "%Y", date_breaks = "1 year")
    

提交回复
热议问题