从结果匹配字符向量的不同数据帧中调用信息

时间:2018-09-26 15:13:49

标签: r dplyr

我正在尝试应用一个功能,将许多公司的财务帐户转换为美元。

与每种货币相关的公司可以在下面找到。

USDfirms <- c("GOOG", "AMZN", "AAPL", "CSCO", "FB", "HP", "IBM", "0992.HK", 
    "MSFT", "CRM", "TWTR", "WB", "ZTE.CN")
CNYfirms <- c("BABA", "BIDU", "1169.HK", "HMI", "3888.HK", "1357.HK", "NTES", 
        "TCEHY", "1810.HK", "0763.HK")
TWDfirms <- c("2357.TW", "2324.TW", "2356.TW", "2498.TW", "3231.TW")
KRWfirms <- c("003550.KS", "005930.KS")
JPYfirms <- c("5563.T", "7752.T")
EURfirms <- "NOK"

因此CNYfirms对应于金融帐户为人民币的中国公司。 dput()是大约30家公司财务的转储,可以找到here

编辑:在此处http://s000.tinyupload.com/download.php?file_id=06545415747486823455&t=0654541574748682345555828链接2

它叫BSISCF

df-dput()称为BSISCF

我还有一个货币换算表:

如下所示;

# A tibble: 1 x 6
  date       cny_usd twd_usd  krw_usd jpy_usd eur_usd
  <date>       <dbl>   <dbl>    <dbl>   <dbl>   <dbl>
1 2018-04-01   0.159  0.0344 0.000943 0.00941    1.23

数据

fx <-structure(list(date = structure(17622, class = "Date"), cny_usd = 0.159228, 
    twd_usd = 0.03442, krw_usd = 0.000943, jpy_usd = 0.009408, 
    eur_usd = 1.232305), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame"), .Names = c("date", "cny_usd", "twd_usd", 
"krw_usd", "jpy_usd", "eur_usd"))

我具有以下功能,而我想做的是使用symbol列并使用ifelse语句。因此,如果symbol列与CNYfirms中的符号匹配,则在exchange.rates列中输入cny_usd表中的fx汇率。对所有符号执行此操作。由于fx表中没有美元汇率,因此NA值将对应于美元公司。

BSISCF <- BSISCF %>% 
  separate(symbol, into = c("ticker", "country"), 
           sep = "[.]", convert = TRUE, remove = FALSE) %>%               # The NA values just correspond to US data
  mutate(exchange.rates = ifelse(symbol == CNYfirms, fx$cny_usd,
                                 ifelse(symbol == TWDfirms, fx$twd_usd,
                                        ifelse(symbol == KRWfirms, fx$krw_usd,
                                               ifelse(symbol == JPYfirms, fx$jpy_usd,
                                                      ifelse(symbol == EURfirms, fx$eur_usd, 0)))))) %>%
  select(exchange.rates, everything())

好吧,这些信息是收集汇率后我将要采取的下一步措施

  mutate(exchange.rates = ifelse(is.na(country), 1, exchange.rates)) %>% 
  mutate_at(.funs = funs(fx = .*exchange.rates), .vars = vars(Cash.And.Cash.Equivalents:Change.In.Cash.and.Cash.Equivalents)) %>%
  mutate(adjusted_fx = adjusted*exchange.rates) #All financial statements and the stocks adjusted price converted into USD

如果我不能很好地解释某些事情,请告诉我。

0 个答案:

没有答案