使用 "last" 和 "lag" 时子集 "incorrect number of dimensions" 出错
Error in subset "incorrect number of dimensions" when using "last" and "lag"
使用 Quantmod 包下载股票数据后,我想对数据进行子集化,并使用 (last / lag) 将 xts 中的最后一行数据与前一行进行比较。
首先,我创建了一个函数来按四分位数对体积进行分类。
其次,我创建了一个新数据集来筛选出列表中哪些股票昨天的交易量为 3(第三四分位数)= "stocks_with3"
现在我想再次对新创建的 "stocks_with3" 数据集进行子集化。
具体来说,我想要得到的是 TRUE/FALSE 比较昨天的 "Open"(使用最后一个)和昨天的 "Close"(使用滞后)。
我想知道的是,昨天成交量在第 3 个四分位数的股票的 "Open" 是否小于或等于昨天前的 "Close"。
但是当 运行 子集时,我收到一条错误消息:"incorrect number of dimensions"
我对子集的方法是使用 last(获取 xts 中的最后一个可用数据)和 lag(将其与紧邻的前一行进行比较)
#Get stock list data
library(quantmod)
library(xts)
Symbols <- c("XOM","MSFT","JNJ","IBM","MRK","BAC","DIS","ORCL","LW","NYT","YELP")
start_date=as.Date("2018-06-01")
getSymbols(Symbols,from=start_date)
stock_data = sapply(.GlobalEnv, is.xts)
all_stocks <- do.call(list, mget(names(stock_data)[stock_data]))
#function to split volume data quartiles into 0-4 results
Volume_q_rank <- function(x) {
stock_name <- stringi::stri_extract(names(x)[1], regex = "^[A-Z]+")
stock_name <- paste0(stock_name, ".Volqrank")
column_names <- c(names(x), stock_name)
x$volqrank <- as.integer(cut(quantmod::Vo(x),
quantile(quantmod::Vo(x),probs=0:4/4),include.lowest=TRUE))
x <- setNames(x, column_names)return(x)
}
all_stocks <- lapply(all_stocks, Volume_q_rank)
#Create a new dataset using names and which with stocks of Volume in the 3rd quartile.
stock3 <- sapply(all_stocks, function(x) {last(x[, grep("\.Volqrank",names(x))]) == 3})
stocks_with3 <- names(which(stock3 == TRUE))
#Here is when I get the error.
stock3_check <- sapply(stocks_with3, function(x) {last(x[, grep("\.Open",names(x))]) <= lag(x[, grep("\.Close", 1), names(x)])})
#Expected result could be the same or running this for a single stock but applied to all the stocks in the list:
last(all_stocks$MSFT$MSFT.Open) <= lag(all_stocks$MSFT$MSFT.Close, 1)
#But I'm having the error when trying to apply to whole list using "sapply" "last" and "lag"
Any suggestion will be appreciated.
Thank you very much.
你的 sapply 函数有 2 个错误。首先,您尝试使用字符向量 (stock_with3) 而不是列表 (all_stocks)。其次,sapply 中使用的函数不正确。滞后右括号在 grep 之前。
这应该有效。
stock3_check <- sapply(all_stocks[stocks_with3], function(x) {
last(x[, grep("\.Open", names(x))]) <= lag(x[, grep("\.Close", names(x))])
})
补充评论
我不确定你想用这段代码实现什么。至于检索数据,下面的代码更容易阅读,并且不会先将所有对象放入 R 会话中,然后再将它们放入列表中:
my_stock_data <- lapply(Symbols , getSymbols, auto.assign = FALSE)
names(my_stock_data) <- Symbols
使用 Quantmod 包下载股票数据后,我想对数据进行子集化,并使用 (last / lag) 将 xts 中的最后一行数据与前一行进行比较。
首先,我创建了一个函数来按四分位数对体积进行分类。
其次,我创建了一个新数据集来筛选出列表中哪些股票昨天的交易量为 3(第三四分位数)= "stocks_with3"
现在我想再次对新创建的 "stocks_with3" 数据集进行子集化。
具体来说,我想要得到的是 TRUE/FALSE 比较昨天的 "Open"(使用最后一个)和昨天的 "Close"(使用滞后)。
我想知道的是,昨天成交量在第 3 个四分位数的股票的 "Open" 是否小于或等于昨天前的 "Close"。
但是当 运行 子集时,我收到一条错误消息:"incorrect number of dimensions"
我对子集的方法是使用 last(获取 xts 中的最后一个可用数据)和 lag(将其与紧邻的前一行进行比较)
#Get stock list data
library(quantmod)
library(xts)
Symbols <- c("XOM","MSFT","JNJ","IBM","MRK","BAC","DIS","ORCL","LW","NYT","YELP")
start_date=as.Date("2018-06-01")
getSymbols(Symbols,from=start_date)
stock_data = sapply(.GlobalEnv, is.xts)
all_stocks <- do.call(list, mget(names(stock_data)[stock_data]))
#function to split volume data quartiles into 0-4 results
Volume_q_rank <- function(x) {
stock_name <- stringi::stri_extract(names(x)[1], regex = "^[A-Z]+")
stock_name <- paste0(stock_name, ".Volqrank")
column_names <- c(names(x), stock_name)
x$volqrank <- as.integer(cut(quantmod::Vo(x),
quantile(quantmod::Vo(x),probs=0:4/4),include.lowest=TRUE))
x <- setNames(x, column_names)return(x)
}
all_stocks <- lapply(all_stocks, Volume_q_rank)
#Create a new dataset using names and which with stocks of Volume in the 3rd quartile.
stock3 <- sapply(all_stocks, function(x) {last(x[, grep("\.Volqrank",names(x))]) == 3})
stocks_with3 <- names(which(stock3 == TRUE))
#Here is when I get the error.
stock3_check <- sapply(stocks_with3, function(x) {last(x[, grep("\.Open",names(x))]) <= lag(x[, grep("\.Close", 1), names(x)])})
#Expected result could be the same or running this for a single stock but applied to all the stocks in the list:
last(all_stocks$MSFT$MSFT.Open) <= lag(all_stocks$MSFT$MSFT.Close, 1)
#But I'm having the error when trying to apply to whole list using "sapply" "last" and "lag"
Any suggestion will be appreciated.
Thank you very much.
你的 sapply 函数有 2 个错误。首先,您尝试使用字符向量 (stock_with3) 而不是列表 (all_stocks)。其次,sapply 中使用的函数不正确。滞后右括号在 grep 之前。
这应该有效。
stock3_check <- sapply(all_stocks[stocks_with3], function(x) {
last(x[, grep("\.Open", names(x))]) <= lag(x[, grep("\.Close", names(x))])
})
补充评论
我不确定你想用这段代码实现什么。至于检索数据,下面的代码更容易阅读,并且不会先将所有对象放入 R 会话中,然后再将它们放入列表中:
my_stock_data <- lapply(Symbols , getSymbols, auto.assign = FALSE)
names(my_stock_data) <- Symbols