Rick
Rick

Reputation: 181

Error in subset "incorrect number of dimensions" when using "last" and "lag"

After downloading stocks data using Quantmod package I want to subset the data and also compare the last row data in the xts with the previous row using (last / lag).

First I created a function to classify the volume in its quartile.

Second I create a new dataset to filter out which stocks in the list get yesterday a volume of 3(3rd quartile) = "stocks_with3"

Now I'd like to subset again the newly created "stocks_with3" dataset.

Specifically what I'm trying to get is TRUE/FALSE of comparing the "Open" of Yesterday (using last) and the "Close" of before yesterday "(using lag).

Exactly what I'm trying to get is if the "Open" was less or equal than the "Close" before yesterday of the stocks that yesterday had a volume in the 3rd quartile.

But when running the subset I'm getting an error message: "incorrect number of dimensions"

My approach for the subset is using last(to get the last available data in the xts) and lag ( to compare it with the immediately previous row)

#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.

Upvotes: 0

Views: 580

Answers (1)

phiver
phiver

Reputation: 23598

You have 2 mistakes in your sapply function. First you are trying use a character vector (stock_with3) instead of a list (all_stocks). Second the function used inside the sapply is incorrect. the lag closing bracket is before the grep.

This should work.

stock3_check <- sapply(all_stocks[stocks_with3], function(x) {
  last(x[, grep("\\.Open", names(x))]) <= lag(x[, grep("\\.Close", names(x))])
  })

additional comments

I'm not sure what you are trying to achieve with this code. As for retrieving your data, the following code is easier to read, and doesn't first put all the objects in your R session and then you putting them into a list:

my_stock_data <- lapply(Symbols , getSymbols, auto.assign = FALSE)
names(my_stock_data) <- Symbols

Upvotes: 3

Related Questions