BatchGetSymbols - reshape output

不羁的心 提交于 2020-06-09 05:20:53

问题


I like to use the advanted of BatchgetSymbols. Any advice how I can best manipulate the output to receive the format below?

symbols_RP <- c('VDNR.L','VEUD.L','VDEM.L','IDTL.L','IEMB.L','GLRE.L','IGLN.L')
#Setting price download date range
from_date <- as.Date('2019-01-01')
to_date <- as.Date(Sys.Date())



get.symbol.adjclose <- function(ticker) {
    l.out <-  BatchGetSymbols(symbols_RP, first.date = from_date, last.date = to_date, do.cache=TRUE, freq.data = "daily", do.complete.data = TRUE, do.fill.missing.prices = TRUE, be.quiet = FALSE)
return(l.out$df.tickers) 
}

prices <- get.symbol.adjclose(symbols_RP)

Output Batchgetsymbols

$df.tickers
    price.open price.high price.low price.close volume price.adjusted   ref.date ticker ret.adjusted.prices ret.closing.prices
1      60.6000    61.7950   60.4000     61.5475   4717       60.59111 2019-01-02 VDNR.L                  NA                 NA
2      60.7200    60.9000   60.5500     60.6650  22015       59.72233 2019-01-03 VDNR.L       -1.433838e-02      -1.433852e-02
3      60.9050    60.9500   60.9050     61.8875   1010       60.92583 2019-01-04 VDNR.L        2.015164e-02       2.015165e-02
4      62.3450    62.7850   62.3400     62.7300    820       61.75524 2019-01-07 VDNR.L        1.361339e-02       1.361340e-02

Desired output below:

               VTI     PUTW      VEA      VWO      TLT     VNQI    GLD      EMB      UST  FTAL
2019-01-02 124.6962 25.18981 35.72355 36.92347 118.6449 48.25209 121.33 97.70655 55.18464 45.76
2019-01-03 121.8065 25.05184 35.43429 36.34457 119.9950 48.32627 122.43 98.12026 56.01122 45.54
2019-01-04 125.8384 25.39677 36.52383 37.49271 118.6061 49.38329 121.44 98.86311 55.10592 46.63
2019-01-07 127.1075 25.57416 36.63954 37.56989 118.2564 49.67072 121.86 99.28625 54.81071 46.54
2019-01-08 128.4157 25.61358 36.89987 37.78215 117.9456 50.06015 121.53 99.21103 54.54502 47.05
2019-01-09 129.0210 25.56431 37.35305 38.33209 117.7610 50.39395 122.31 99.38966 54.56470 47.29

as I know from other languages, I could use for loop, but I know there is faster ways in r.

Maybe one could hint me the r-way?

Improved version:

get.symbol.adjclose <- function(ticker) {
    l.out <-  BatchGetSymbols(symbols_RP, first.date = from_date, last.date = to_date, do.cache=TRUE, freq.data = "daily", do.complete.data = TRUE, do.fill.missing.prices = TRUE, be.quiet = FALSE)
    return(as.data.frame(l.out$df.tickers[c("ticker","ref.date","price.open","price.high","price.low","price.close","volume","price.adjusted")]))
}

回答1:


Using dplyr and tidyr. I'm selecting price.adjusted, but you can use any of the prices you need.

library(dplyr)
library(tidyr)

prices %>% 
  select(ref.date, ticker, price.adjusted) %>% # select columns before pivot_wider
  pivot_wider(names_from = ticker, values_from = price.adjusted)

# A tibble: 352 x 7
   ref.date   GLRE.L IDTL.L IGLN.L VDEM.L VDNR.L VEUD.L
   <date>      <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
 1 2019-01-02   NA    NA      25.2   51.0   60.6   30.2
 2 2019-01-03   32.2   4.50   25.3   50.3   59.7   30.1
 3 2019-01-04   32.6   4.47   25.2   51.7   60.9   30.9
 4 2019-01-07   32.8   4.47   25.3   51.8   61.8   31.0
 5 2019-01-08   32.8   4.44   25.2   51.9   62.0   31.3
 6 2019-01-09   33.3   4.43   25.3   53.0   62.7   31.7
 7 2019-01-10   33.5   4.41   25.3   53.2   62.7   31.7
 8 2019-01-11   33.8   4.40   25.3   53.1   62.8   31.6
 9 2019-01-14   33.8   4.41   25.3   52.7   62.7   31.4
10 2019-01-15   34.0   4.41   25.3   53.1   63.1   31.4
# ... with 342 more rows

Note from BatchGetSymbols :

IEMB.L OUT: not enough data (thresh.bad.data = 75%)



来源:https://stackoverflow.com/questions/61985499/batchgetsymbols-reshape-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!