使用函数 Delt() 添加 return 的股票数据到数据框列表
Add return of stock data with function Delt() to a list of dataframe
我有一个包含股票数据的数据框列表,我想向列表中的每个数据框添加一个包含 return 股票价格的新列。数据框如下所示:
ABB.ST.Open ABB.ST.High ABB.ST.Low ABB.ST.Close ABB.ST.Volume ABB.ST.Adjusted Date
1 166.4 167.4 165.5 166.0 944525 162.1181 2015-01-02
2 165.6 166.6 164.4 164.4 1013670 160.5555 2015-01-05
3 161.2 161.6 159.1 159.1 2546062 155.3794 2015-01-07
4 160.3 163.3 159.2 163.0 1946157 159.1882 2015-01-08
5 162.8 162.8 160.0 160.6 1885969 156.8444 2015-01-09
6 161.5 162.6 160.6 161.1 1795966 157.3327 2015-01-12
7 159.8 160.6 158.3 160.0 2445040 156.2584 2015-01-13
Delt.1.arithmetic
1 NA
2 -0.0066959806
3 -0.0127628163
4 0.0168292916
5 -0.0002389207
6 0.0141549696
7 -0.0044953302
这一直持续到今天的日期,即大约 1300 行。
现在想用包 quantmod
.
中的函数 Delt
添加第一列 ABB.ST.Open
的 return
当我仅将此代码应用于列表之外的数据框时,此代码有效:
ABB.ST$Return <- Delt(ABB.ST[,1])
但是如何将此列添加到 list
中的所有数据框中?
我的列表名称是 master_df
,我试过这个代码,但它不起作用...
Return <- function(x){
master_df[[x]]$Return <- Delt(master_df[[x]][,1])
}
test2 <- lapply(master_df, Return(x))
更新:
我在列表中有 data.frame,我的列表名称是 master_df
,这是我从 yahoo finance 下载数据的代码:
as.matrix(LargeCapOMXS[,7])
是我来自 yahoo
的符号名称列表
Stocklist <- as.matrix(LargeCapOMXS[,7]) #get names of stocks
master_df <- list() #create list to fill with data
for(i in seq(length(Stocklist))){ #loop to fill list with downloaded stock data, by company
Stockindex = Stocklist[i]
getSymbols(Stockindex, src="yahoo", from="2015-01-01", to="2020-01-01", verbose = TRUE)
master_df[[i]] <- as.data.frame(get(Stockindex))
master_df[[i]]$Date = row.names(master_df[[i]])
row.names( master_df[[i]]) = NULL
}
}
我设法用以下 for 循环解决了这个问题:
for(i in seq(length(master_df))){
master_df[[i]]$Return <- Delt(master_df[[i]][,1])
}
有没有人有使用应用功能的想法?这个 for 循环相当快,但出于好奇。
下面是一些代码,用于从 yahoo 获取数据,将其放入列表中,并将 Delt 列添加到每个 xts 对象。无需创建 data.frames。您以后可以随时执行此操作。
library(quantmod)
stock_list <- c("ABB", "ABT")
master_df <- lapply(stock_list, getSymbols, from = "2015-01-01", to = "2020-01-01", auto.assign = FALSE)
names(master_df) <- stock_list
# need to merge the xts with the outcome of Delt as Delt only returns 1 column
# function Op selects the open column.
master_df <- lapply(master_df, function(x) merge(x, Delt(Op(x))))
如果你想要一个巨大的 data.frame。
或者使用 tidyquant
library(tidyquant)
library(dplyr)
master_df_tq <- tq_get(stock_list)
master_df_tq <- master_df_tq %>%
group_by(symbol) %>%
tq_mutate(select = open,
mutate_fun = Delt)
我有一个包含股票数据的数据框列表,我想向列表中的每个数据框添加一个包含 return 股票价格的新列。数据框如下所示:
ABB.ST.Open ABB.ST.High ABB.ST.Low ABB.ST.Close ABB.ST.Volume ABB.ST.Adjusted Date
1 166.4 167.4 165.5 166.0 944525 162.1181 2015-01-02
2 165.6 166.6 164.4 164.4 1013670 160.5555 2015-01-05
3 161.2 161.6 159.1 159.1 2546062 155.3794 2015-01-07
4 160.3 163.3 159.2 163.0 1946157 159.1882 2015-01-08
5 162.8 162.8 160.0 160.6 1885969 156.8444 2015-01-09
6 161.5 162.6 160.6 161.1 1795966 157.3327 2015-01-12
7 159.8 160.6 158.3 160.0 2445040 156.2584 2015-01-13
Delt.1.arithmetic
1 NA
2 -0.0066959806
3 -0.0127628163
4 0.0168292916
5 -0.0002389207
6 0.0141549696
7 -0.0044953302
这一直持续到今天的日期,即大约 1300 行。
现在想用包 quantmod
.
Delt
添加第一列 ABB.ST.Open
的 return
当我仅将此代码应用于列表之外的数据框时,此代码有效:
ABB.ST$Return <- Delt(ABB.ST[,1])
但是如何将此列添加到 list
中的所有数据框中?
我的列表名称是 master_df
,我试过这个代码,但它不起作用...
Return <- function(x){
master_df[[x]]$Return <- Delt(master_df[[x]][,1])
}
test2 <- lapply(master_df, Return(x))
更新:
我在列表中有 data.frame,我的列表名称是 master_df
,这是我从 yahoo finance 下载数据的代码:
as.matrix(LargeCapOMXS[,7])
是我来自 yahoo
Stocklist <- as.matrix(LargeCapOMXS[,7]) #get names of stocks
master_df <- list() #create list to fill with data
for(i in seq(length(Stocklist))){ #loop to fill list with downloaded stock data, by company
Stockindex = Stocklist[i]
getSymbols(Stockindex, src="yahoo", from="2015-01-01", to="2020-01-01", verbose = TRUE)
master_df[[i]] <- as.data.frame(get(Stockindex))
master_df[[i]]$Date = row.names(master_df[[i]])
row.names( master_df[[i]]) = NULL
}
}
我设法用以下 for 循环解决了这个问题:
for(i in seq(length(master_df))){
master_df[[i]]$Return <- Delt(master_df[[i]][,1])
}
有没有人有使用应用功能的想法?这个 for 循环相当快,但出于好奇。
下面是一些代码,用于从 yahoo 获取数据,将其放入列表中,并将 Delt 列添加到每个 xts 对象。无需创建 data.frames。您以后可以随时执行此操作。
library(quantmod)
stock_list <- c("ABB", "ABT")
master_df <- lapply(stock_list, getSymbols, from = "2015-01-01", to = "2020-01-01", auto.assign = FALSE)
names(master_df) <- stock_list
# need to merge the xts with the outcome of Delt as Delt only returns 1 column
# function Op selects the open column.
master_df <- lapply(master_df, function(x) merge(x, Delt(Op(x))))
如果你想要一个巨大的 data.frame。
或者使用 tidyquantlibrary(tidyquant)
library(dplyr)
master_df_tq <- tq_get(stock_list)
master_df_tq <- master_df_tq %>%
group_by(symbol) %>%
tq_mutate(select = open,
mutate_fun = Delt)