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.
r subset quantmod
add a comment |
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.
r subset quantmod
add a comment |
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.
r subset quantmod
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.
r subset quantmod
r subset quantmod
asked Dec 28 '18 at 17:45
AL.AL.
798
798
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53962372%2ferror-in-subset-incorrect-number-of-dimensions-when-using-last-and-lag%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
answered Dec 28 '18 at 18:23
phiverphiver
13k92634
13k92634
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53962372%2ferror-in-subset-incorrect-number-of-dimensions-when-using-last-and-lag%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown