循环 new.env() R (quantmod)
loop through new.env() R (quantmod)
我是 R、loop 和 quantmod 的新手。
我正在尝试构建一个可以更轻松地用于计算的数据库(以列格式)。
我想知道您将如何使用 "loop" 来自动执行以下过程。
我只是将两个数据集绑定在一起。
如您所见,我使用 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)
非常感谢,
舒普
我们可以使用 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
包 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>
我是 R、loop 和 quantmod 的新手。 我正在尝试构建一个可以更轻松地用于计算的数据库(以列格式)。
我想知道您将如何使用 "loop" 来自动执行以下过程。 我只是将两个数据集绑定在一起。
如您所见,我使用 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)
非常感谢,
舒普
我们可以使用 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
包 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>