Trying to use the window function from R











up vote
3
down vote

favorite












This is my dataframe:



structure(list(dates = structure(c(16162, 16161, 16160, 16157, 
16156, 16155, 16154, 16153, 16150, 16149, 16148, 16147, 16146,
16143, 16142, 16141, 16140, 16139, 16136, 16135, 16134, 16129,
16128, 16127, 16126, 16125, 16122, 16121, 16120, 16119, 16118,
16115, 16114, 16113, 16112, 16111, 16108, 16107, 16106, 16105,
16104, 16101, 16100, 16099, 16098, 16097, 16094, 16093, 16092,
16091), class = "Date"), VALE5 = c(28.29, 28.26, 28.35, 27.81,
27.85, 27.5, 27.61, 27.16, 27.2, 26.64, 26.57, 26.55, 26, 26.1,
25.9, 26.46, 26.1, 26.37, 27.09, 28.11, 28.11, 29.09, 29.31,
29.02, 29, 29.76, 30.61, 30.59, 30.9, 30.6, 30.74, 30.96, 30.76,
30.79, 30.77, 30.44, 30.66, 30.8, 29.94, 29.58, 29.1, 30, 29.76,
29.96, 28.88, 28.54, 28.63, 28.15, 28.91, 28.48)), row.names = c(NA,
50L), class = "data.frame")


I want to set the window function like this:



window(sample,start=c(2014,03,26),end=c(2014,04,02))


What is wrong with my sample dataframe? The dates collumn has the class Date . It is not enough to use the window function?



How can I fix it?



Many thanks.










share|improve this question




















  • 1




    I think window is supposed to be used for ts class time series objects, while you have a data.frame. Are you just trying to select rows in a particular period like sample[sample$dates >= "2014-03-26" & sample$dates <= "2014-04-02",] ?
    – thelatemail
    Nov 10 at 23:06












  • @thelatemail thanks. Yes. But after select the dates I will roll this window. I will estimate a OLS regression. Is it possible to do the rolling with sample[sample$dates >= "2014-03-26" & sample$dates <= "2014-04-02",] idea?
    – Laura
    Nov 10 at 23:12










  • This should be possible but is not something I've personally done - you might want to look into the zoo package which has many roll* functions, in particular rollapply which will allow you to apply an arbitrary function to a moving/rolling window of values. See here for an example - stackoverflow.com/questions/13324362/…
    – thelatemail
    Nov 10 at 23:16












  • I edited the sample size. How could I have windows of the size 10 moving each day. This is what I want.
    – Laura
    Nov 10 at 23:16















up vote
3
down vote

favorite












This is my dataframe:



structure(list(dates = structure(c(16162, 16161, 16160, 16157, 
16156, 16155, 16154, 16153, 16150, 16149, 16148, 16147, 16146,
16143, 16142, 16141, 16140, 16139, 16136, 16135, 16134, 16129,
16128, 16127, 16126, 16125, 16122, 16121, 16120, 16119, 16118,
16115, 16114, 16113, 16112, 16111, 16108, 16107, 16106, 16105,
16104, 16101, 16100, 16099, 16098, 16097, 16094, 16093, 16092,
16091), class = "Date"), VALE5 = c(28.29, 28.26, 28.35, 27.81,
27.85, 27.5, 27.61, 27.16, 27.2, 26.64, 26.57, 26.55, 26, 26.1,
25.9, 26.46, 26.1, 26.37, 27.09, 28.11, 28.11, 29.09, 29.31,
29.02, 29, 29.76, 30.61, 30.59, 30.9, 30.6, 30.74, 30.96, 30.76,
30.79, 30.77, 30.44, 30.66, 30.8, 29.94, 29.58, 29.1, 30, 29.76,
29.96, 28.88, 28.54, 28.63, 28.15, 28.91, 28.48)), row.names = c(NA,
50L), class = "data.frame")


I want to set the window function like this:



window(sample,start=c(2014,03,26),end=c(2014,04,02))


What is wrong with my sample dataframe? The dates collumn has the class Date . It is not enough to use the window function?



How can I fix it?



Many thanks.










share|improve this question




















  • 1




    I think window is supposed to be used for ts class time series objects, while you have a data.frame. Are you just trying to select rows in a particular period like sample[sample$dates >= "2014-03-26" & sample$dates <= "2014-04-02",] ?
    – thelatemail
    Nov 10 at 23:06












  • @thelatemail thanks. Yes. But after select the dates I will roll this window. I will estimate a OLS regression. Is it possible to do the rolling with sample[sample$dates >= "2014-03-26" & sample$dates <= "2014-04-02",] idea?
    – Laura
    Nov 10 at 23:12










  • This should be possible but is not something I've personally done - you might want to look into the zoo package which has many roll* functions, in particular rollapply which will allow you to apply an arbitrary function to a moving/rolling window of values. See here for an example - stackoverflow.com/questions/13324362/…
    – thelatemail
    Nov 10 at 23:16












  • I edited the sample size. How could I have windows of the size 10 moving each day. This is what I want.
    – Laura
    Nov 10 at 23:16













up vote
3
down vote

favorite









up vote
3
down vote

favorite











This is my dataframe:



structure(list(dates = structure(c(16162, 16161, 16160, 16157, 
16156, 16155, 16154, 16153, 16150, 16149, 16148, 16147, 16146,
16143, 16142, 16141, 16140, 16139, 16136, 16135, 16134, 16129,
16128, 16127, 16126, 16125, 16122, 16121, 16120, 16119, 16118,
16115, 16114, 16113, 16112, 16111, 16108, 16107, 16106, 16105,
16104, 16101, 16100, 16099, 16098, 16097, 16094, 16093, 16092,
16091), class = "Date"), VALE5 = c(28.29, 28.26, 28.35, 27.81,
27.85, 27.5, 27.61, 27.16, 27.2, 26.64, 26.57, 26.55, 26, 26.1,
25.9, 26.46, 26.1, 26.37, 27.09, 28.11, 28.11, 29.09, 29.31,
29.02, 29, 29.76, 30.61, 30.59, 30.9, 30.6, 30.74, 30.96, 30.76,
30.79, 30.77, 30.44, 30.66, 30.8, 29.94, 29.58, 29.1, 30, 29.76,
29.96, 28.88, 28.54, 28.63, 28.15, 28.91, 28.48)), row.names = c(NA,
50L), class = "data.frame")


I want to set the window function like this:



window(sample,start=c(2014,03,26),end=c(2014,04,02))


What is wrong with my sample dataframe? The dates collumn has the class Date . It is not enough to use the window function?



How can I fix it?



Many thanks.










share|improve this question















This is my dataframe:



structure(list(dates = structure(c(16162, 16161, 16160, 16157, 
16156, 16155, 16154, 16153, 16150, 16149, 16148, 16147, 16146,
16143, 16142, 16141, 16140, 16139, 16136, 16135, 16134, 16129,
16128, 16127, 16126, 16125, 16122, 16121, 16120, 16119, 16118,
16115, 16114, 16113, 16112, 16111, 16108, 16107, 16106, 16105,
16104, 16101, 16100, 16099, 16098, 16097, 16094, 16093, 16092,
16091), class = "Date"), VALE5 = c(28.29, 28.26, 28.35, 27.81,
27.85, 27.5, 27.61, 27.16, 27.2, 26.64, 26.57, 26.55, 26, 26.1,
25.9, 26.46, 26.1, 26.37, 27.09, 28.11, 28.11, 29.09, 29.31,
29.02, 29, 29.76, 30.61, 30.59, 30.9, 30.6, 30.74, 30.96, 30.76,
30.79, 30.77, 30.44, 30.66, 30.8, 29.94, 29.58, 29.1, 30, 29.76,
29.96, 28.88, 28.54, 28.63, 28.15, 28.91, 28.48)), row.names = c(NA,
50L), class = "data.frame")


I want to set the window function like this:



window(sample,start=c(2014,03,26),end=c(2014,04,02))


What is wrong with my sample dataframe? The dates collumn has the class Date . It is not enough to use the window function?



How can I fix it?



Many thanks.







r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 23:15

























asked Nov 10 at 23:04









Laura

35319




35319








  • 1




    I think window is supposed to be used for ts class time series objects, while you have a data.frame. Are you just trying to select rows in a particular period like sample[sample$dates >= "2014-03-26" & sample$dates <= "2014-04-02",] ?
    – thelatemail
    Nov 10 at 23:06












  • @thelatemail thanks. Yes. But after select the dates I will roll this window. I will estimate a OLS regression. Is it possible to do the rolling with sample[sample$dates >= "2014-03-26" & sample$dates <= "2014-04-02",] idea?
    – Laura
    Nov 10 at 23:12










  • This should be possible but is not something I've personally done - you might want to look into the zoo package which has many roll* functions, in particular rollapply which will allow you to apply an arbitrary function to a moving/rolling window of values. See here for an example - stackoverflow.com/questions/13324362/…
    – thelatemail
    Nov 10 at 23:16












  • I edited the sample size. How could I have windows of the size 10 moving each day. This is what I want.
    – Laura
    Nov 10 at 23:16














  • 1




    I think window is supposed to be used for ts class time series objects, while you have a data.frame. Are you just trying to select rows in a particular period like sample[sample$dates >= "2014-03-26" & sample$dates <= "2014-04-02",] ?
    – thelatemail
    Nov 10 at 23:06












  • @thelatemail thanks. Yes. But after select the dates I will roll this window. I will estimate a OLS regression. Is it possible to do the rolling with sample[sample$dates >= "2014-03-26" & sample$dates <= "2014-04-02",] idea?
    – Laura
    Nov 10 at 23:12










  • This should be possible but is not something I've personally done - you might want to look into the zoo package which has many roll* functions, in particular rollapply which will allow you to apply an arbitrary function to a moving/rolling window of values. See here for an example - stackoverflow.com/questions/13324362/…
    – thelatemail
    Nov 10 at 23:16












  • I edited the sample size. How could I have windows of the size 10 moving each day. This is what I want.
    – Laura
    Nov 10 at 23:16








1




1




I think window is supposed to be used for ts class time series objects, while you have a data.frame. Are you just trying to select rows in a particular period like sample[sample$dates >= "2014-03-26" & sample$dates <= "2014-04-02",] ?
– thelatemail
Nov 10 at 23:06






I think window is supposed to be used for ts class time series objects, while you have a data.frame. Are you just trying to select rows in a particular period like sample[sample$dates >= "2014-03-26" & sample$dates <= "2014-04-02",] ?
– thelatemail
Nov 10 at 23:06














@thelatemail thanks. Yes. But after select the dates I will roll this window. I will estimate a OLS regression. Is it possible to do the rolling with sample[sample$dates >= "2014-03-26" & sample$dates <= "2014-04-02",] idea?
– Laura
Nov 10 at 23:12




@thelatemail thanks. Yes. But after select the dates I will roll this window. I will estimate a OLS regression. Is it possible to do the rolling with sample[sample$dates >= "2014-03-26" & sample$dates <= "2014-04-02",] idea?
– Laura
Nov 10 at 23:12












This should be possible but is not something I've personally done - you might want to look into the zoo package which has many roll* functions, in particular rollapply which will allow you to apply an arbitrary function to a moving/rolling window of values. See here for an example - stackoverflow.com/questions/13324362/…
– thelatemail
Nov 10 at 23:16






This should be possible but is not something I've personally done - you might want to look into the zoo package which has many roll* functions, in particular rollapply which will allow you to apply an arbitrary function to a moving/rolling window of values. See here for an example - stackoverflow.com/questions/13324362/…
– thelatemail
Nov 10 at 23:16














I edited the sample size. How could I have windows of the size 10 moving each day. This is what I want.
– Laura
Nov 10 at 23:16




I edited the sample size. How could I have windows of the size 10 moving each day. This is what I want.
– Laura
Nov 10 at 23:16












1 Answer
1






active

oldest

votes

















up vote
4
down vote













1) window.zoo window is meant for time series objects like ts and zoo. ts is not appropriate for daily data but you can convert sample to a zoo object and then run it like this:



library(zoo)

z <- read.zoo(sample)
window(z, start = "2014-03-26", end = "2014-04-02")


giving:



           BBAS3 BBDC3 VALE5
2014-03-26 21.11 31.53 27.50
2014-03-27 22.51 33.38 27.85
2014-03-28 22.22 33.20 27.81
2014-03-31 22.80 33.59 28.35
2014-04-01 22.78 33.75 28.26
2014-04-02 22.85 33.82 28.29


2) subset This base approach would also work but does not use window:



subset(sample, dates >= "2014-03-26" & dates <= "2014-04-02")


3) xts With xts this notation can be used. z is from (1) above:



library(xts)

x <- as.xts(z)
x["2014-03-26/2014-04-02"]





share|improve this answer























  • Thanks @G. Grothendieck. It would be possible to move this window by the dates? I will set the last ten dates and running a lm regression. After another window with ten dates and another lm regressio.
    – Laura
    Nov 10 at 23:19










  • You can do things like this: rollapplyr(z, 3, function(x) coef(lm(as.data.frame(x))), by.column = FALSE)
    – G. Grothendieck
    Nov 10 at 23:22











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',
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244287%2ftrying-to-use-the-window-function-from-r%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








up vote
4
down vote













1) window.zoo window is meant for time series objects like ts and zoo. ts is not appropriate for daily data but you can convert sample to a zoo object and then run it like this:



library(zoo)

z <- read.zoo(sample)
window(z, start = "2014-03-26", end = "2014-04-02")


giving:



           BBAS3 BBDC3 VALE5
2014-03-26 21.11 31.53 27.50
2014-03-27 22.51 33.38 27.85
2014-03-28 22.22 33.20 27.81
2014-03-31 22.80 33.59 28.35
2014-04-01 22.78 33.75 28.26
2014-04-02 22.85 33.82 28.29


2) subset This base approach would also work but does not use window:



subset(sample, dates >= "2014-03-26" & dates <= "2014-04-02")


3) xts With xts this notation can be used. z is from (1) above:



library(xts)

x <- as.xts(z)
x["2014-03-26/2014-04-02"]





share|improve this answer























  • Thanks @G. Grothendieck. It would be possible to move this window by the dates? I will set the last ten dates and running a lm regression. After another window with ten dates and another lm regressio.
    – Laura
    Nov 10 at 23:19










  • You can do things like this: rollapplyr(z, 3, function(x) coef(lm(as.data.frame(x))), by.column = FALSE)
    – G. Grothendieck
    Nov 10 at 23:22















up vote
4
down vote













1) window.zoo window is meant for time series objects like ts and zoo. ts is not appropriate for daily data but you can convert sample to a zoo object and then run it like this:



library(zoo)

z <- read.zoo(sample)
window(z, start = "2014-03-26", end = "2014-04-02")


giving:



           BBAS3 BBDC3 VALE5
2014-03-26 21.11 31.53 27.50
2014-03-27 22.51 33.38 27.85
2014-03-28 22.22 33.20 27.81
2014-03-31 22.80 33.59 28.35
2014-04-01 22.78 33.75 28.26
2014-04-02 22.85 33.82 28.29


2) subset This base approach would also work but does not use window:



subset(sample, dates >= "2014-03-26" & dates <= "2014-04-02")


3) xts With xts this notation can be used. z is from (1) above:



library(xts)

x <- as.xts(z)
x["2014-03-26/2014-04-02"]





share|improve this answer























  • Thanks @G. Grothendieck. It would be possible to move this window by the dates? I will set the last ten dates and running a lm regression. After another window with ten dates and another lm regressio.
    – Laura
    Nov 10 at 23:19










  • You can do things like this: rollapplyr(z, 3, function(x) coef(lm(as.data.frame(x))), by.column = FALSE)
    – G. Grothendieck
    Nov 10 at 23:22













up vote
4
down vote










up vote
4
down vote









1) window.zoo window is meant for time series objects like ts and zoo. ts is not appropriate for daily data but you can convert sample to a zoo object and then run it like this:



library(zoo)

z <- read.zoo(sample)
window(z, start = "2014-03-26", end = "2014-04-02")


giving:



           BBAS3 BBDC3 VALE5
2014-03-26 21.11 31.53 27.50
2014-03-27 22.51 33.38 27.85
2014-03-28 22.22 33.20 27.81
2014-03-31 22.80 33.59 28.35
2014-04-01 22.78 33.75 28.26
2014-04-02 22.85 33.82 28.29


2) subset This base approach would also work but does not use window:



subset(sample, dates >= "2014-03-26" & dates <= "2014-04-02")


3) xts With xts this notation can be used. z is from (1) above:



library(xts)

x <- as.xts(z)
x["2014-03-26/2014-04-02"]





share|improve this answer














1) window.zoo window is meant for time series objects like ts and zoo. ts is not appropriate for daily data but you can convert sample to a zoo object and then run it like this:



library(zoo)

z <- read.zoo(sample)
window(z, start = "2014-03-26", end = "2014-04-02")


giving:



           BBAS3 BBDC3 VALE5
2014-03-26 21.11 31.53 27.50
2014-03-27 22.51 33.38 27.85
2014-03-28 22.22 33.20 27.81
2014-03-31 22.80 33.59 28.35
2014-04-01 22.78 33.75 28.26
2014-04-02 22.85 33.82 28.29


2) subset This base approach would also work but does not use window:



subset(sample, dates >= "2014-03-26" & dates <= "2014-04-02")


3) xts With xts this notation can be used. z is from (1) above:



library(xts)

x <- as.xts(z)
x["2014-03-26/2014-04-02"]






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 18:01

























answered Nov 10 at 23:16









G. Grothendieck

142k9124227




142k9124227












  • Thanks @G. Grothendieck. It would be possible to move this window by the dates? I will set the last ten dates and running a lm regression. After another window with ten dates and another lm regressio.
    – Laura
    Nov 10 at 23:19










  • You can do things like this: rollapplyr(z, 3, function(x) coef(lm(as.data.frame(x))), by.column = FALSE)
    – G. Grothendieck
    Nov 10 at 23:22


















  • Thanks @G. Grothendieck. It would be possible to move this window by the dates? I will set the last ten dates and running a lm regression. After another window with ten dates and another lm regressio.
    – Laura
    Nov 10 at 23:19










  • You can do things like this: rollapplyr(z, 3, function(x) coef(lm(as.data.frame(x))), by.column = FALSE)
    – G. Grothendieck
    Nov 10 at 23:22
















Thanks @G. Grothendieck. It would be possible to move this window by the dates? I will set the last ten dates and running a lm regression. After another window with ten dates and another lm regressio.
– Laura
Nov 10 at 23:19




Thanks @G. Grothendieck. It would be possible to move this window by the dates? I will set the last ten dates and running a lm regression. After another window with ten dates and another lm regressio.
– Laura
Nov 10 at 23:19












You can do things like this: rollapplyr(z, 3, function(x) coef(lm(as.data.frame(x))), by.column = FALSE)
– G. Grothendieck
Nov 10 at 23:22




You can do things like this: rollapplyr(z, 3, function(x) coef(lm(as.data.frame(x))), by.column = FALSE)
– G. Grothendieck
Nov 10 at 23:22


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244287%2ftrying-to-use-the-window-function-from-r%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues