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.
r
add a comment |
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.
r
1
I thinkwindow
is supposed to be used forts
class time series objects, while you have adata.frame
. Are you just trying to select rows in a particular period likesample[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 withsample[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 thezoo
package which has manyroll*
functions, in particularrollapply
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
add a comment |
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.
r
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
r
edited Nov 10 at 23:15
asked Nov 10 at 23:04
Laura
35319
35319
1
I thinkwindow
is supposed to be used forts
class time series objects, while you have adata.frame
. Are you just trying to select rows in a particular period likesample[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 withsample[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 thezoo
package which has manyroll*
functions, in particularrollapply
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
add a comment |
1
I thinkwindow
is supposed to be used forts
class time series objects, while you have adata.frame
. Are you just trying to select rows in a particular period likesample[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 withsample[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 thezoo
package which has manyroll*
functions, in particularrollapply
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
add a comment |
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"]
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
add a comment |
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"]
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
add a comment |
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"]
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
add a comment |
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"]
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"]
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
add a comment |
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
add a comment |
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%2f53244287%2ftrying-to-use-the-window-function-from-r%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
1
I think
window
is supposed to be used forts
class time series objects, while you have adata.frame
. Are you just trying to select rows in a particular period likesample[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 manyroll*
functions, in particularrollapply
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