【问题标题】:loop through new.env() R (quantmod)循环通过 new.env() R (quantmod)
【发布时间】:2017-06-03 14:54:20
【问题描述】:

我是 R、loop 和 quantmod 的新手。 我正在尝试构建一个可以更轻松地用于计算的数据库(以列格式)。

我想知道您将如何使用“循环”来自动化下面的过程。 我只是将两个数据集绑定在一起。

如您所见,我使用 rbind、Google 和 Apple 股票价格将两个数据集绑定在一起。如果我有 100 只股票,这个过程需要很长时间,所以我想知道如何使这个过程自动化?

library(quantmod)
tickers<- c("AAPL", "GOOG")
all<- new.env()
getSymbols(tickers,src="google", env = all, from = Sys.Date()-100, to = Sys.Date())

apple_share<- all$AAPL
colnames(apple_share)<- c("Open", "High", "Low", "Close", "Volume")
apple_share$Ticker<- rep(1, nrow(apple_share))

google_share<- all$GOOG
colnames(google_share)<- c("Open", "High", "Low", "Close", "Volume")
google_share$Ticker<- rep(2, nrow(google_share))

combined_data<- rbind(apple_share,google_share)

非常感谢,

苏普

【问题讨论】:

    标签: r loops quantmod


    【解决方案1】:

    tidyquant 包正是为您要求的任务而创建的。假设您将 quantmod 包更新到版本 0.4-9,这再次允许从 YAHOO 下载价格,tq_get 函数将下载数据并通过“按符号分组”您将获得所需的输出。

    > library(tidyquant)
    stocks <- c("AAPL", "GOOG", "NFLX") %>%
         tq_get(get  = "stock.prices",
                from = "2010-01-01",
                to   = "2015-12-31") %>%
         group_by(symbol)
    
    > stocks
    Source: local data frame [4,527 x 8]
    Groups: symbol [3]
    
    # A tibble: 4,527 x 8
       symbol       date   open   high    low  close    volume
        <chr>     <date>  <dbl>  <dbl>  <dbl>  <dbl>     <dbl>
     1   AAPL 2010-01-04 213.43 214.50 212.38 214.01 123432400
     2   AAPL 2010-01-05 214.60 215.59 213.25 214.38 150476200
     3   AAPL 2010-01-06 214.38 215.23 210.75 210.97 138040000
     4   AAPL 2010-01-07 211.75 212.00 209.05 210.58 119282800
     5   AAPL 2010-01-08 210.30 212.00 209.06 211.98 111902700
     6   AAPL 2010-01-11 212.80 213.00 208.45 210.11 115557400
     7   AAPL 2010-01-12 209.19 209.77 206.42 207.72 148614900
     8   AAPL 2010-01-13 207.87 210.93 204.10 210.65 151473000
     9   AAPL 2010-01-14 210.11 210.46 209.02 209.43 108223500
    10   AAPL 2010-01-15 210.93 211.60 205.87 205.93 148516900
    # ... with 4,517 more rows, and 1 more variables: adjusted <dbl>
    

    【讨论】:

      【解决方案2】:

      我们可以使用mget获取值

      lst <- Map(cbind, mget(tickers, envir = all), Ticker = seq_along(tickers))
      lst <- lapply(lst, function(x) setNames(x, sub("[^.]+\\.", "", names(x))))
      newdat <- do.call(rbind, lst)
      head(newdat)
      #             Open   High    Low  Close   Volume Ticker
      #2017-02-23 137.38 137.48 136.30 136.53 20788186      1
      #2017-02-23 830.12 832.46 822.88 831.33  1472771      2
      #2017-02-24 135.91 136.66 135.28 136.66 21776585      1
      #2017-02-24 827.73 829.00 824.20 828.64  1392202      2
      #2017-02-27 137.14 137.44 136.28 136.93 20257426      1
      #2017-02-27 824.55 830.50 824.00 829.28  1101466      2
      

      【讨论】:

      • 非常感谢!那真的很有帮助。我会更详细地研究这个:)
      • 您好,很抱歉再次打扰您。再次感谢您的代码。有一部分我有点困惑。您写道: setNames(x, sub("[^.]+\\.", "", names(x)), 我假设这是您用来替换列标题的函数,因此它只显示“打开" 而不是 "AAPL.Open"。我想知道 [^.]+\\ 代表什么?我以前从未见过。
      • @ShuopengCui 是根据你的代码将前缀为 'AAPL.Open' 替换为 'Open'。语法[^.]+ 是匹配一个或多个不是.的字符
      猜你喜欢
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      • 2016-02-15
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多