Extracting Elements By Date

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Extracting Elements By Date

Jeff08
Dear R Gurus,

Thanks for any help in advance!

Date.frame: Returns.names

       X       id ticker      date_ adjClose   totret RankStk
258060 258060 13645T10     CP 2001-06-29   18.125 1877.758    

My data frame is in the above format. I would like to filter by period, per id (every 125 days) each consisting of 250 days, I.e. 1-250, 126-375, etc.

One important thing to note is that not all ID's have the same number of dates.

x<-unique(Returns.names$date_) gives me the ordered list of all the dates, so for each period i want it would be x[((n-1)*125+1):((n+1)*125)]

when i tried to filter for just 1 period, it worked fine, but alas, I do not know how to work around the problem that you cannot assign dynamic variables in loops (say I named the variable for the extracted dates Returns.period, it would get overwritten every iteration of the loop, and I cant name something Returns.n, where n is the index for the loop)

Thus, I tried to adjust by dynamically assigning each period to a column. However, I get the error: object 'Returns.period1' not found. And R is not like Java where you can simply declare a variable by typing say Returns.period1.

I do not know how to create a flexible empty matrix that would allow this code to work.

If anyone knows how to do this without a loop, that would be even better!

##Filtering by Period
n<-1
while(n<=19) {
        Returns.period<-lapply(((n-1)*125+1):((n+1)*125), function (i) which(Returns.filter$date_==x[i]))
        Returns.period<-unlist(Returns.period)
        Returns.period1[n,1:(length(Returns.period))]<-Returns.filter[Returns.period,]
        n<-n+1
        }
Reply | Threaded
Open this post in threaded view
|

Re: Extracting Elements By Date

Jeff08
edit: I found out how to declare empty variables in R, but the code still does not work. I get the index out of bounds error since my data is irregular (some have more dates than others, and the matrix will not allow for different sized rows)

Dear R Gurus,

Thanks for any help in advance!

Date.frame: Returns.names

       X       id ticker      date_ adjClose   totret RankStk
258060 258060 13645T10     CP 2001-06-29   18.125 1877.758    

My data frame is in the above format. I would like to filter by period, per id (every 125 days) each consisting of 250 days, I.e. 1-250, 126-375, etc.

One important thing to note is that not all ID's have the same number of dates.

x<-unique(Returns.names$date_) gives me the ordered list of all the dates, so for each period i want it would be x[((n-1)*125+1):((n+1)*125)]

when i tried to filter for just 1 period, it worked fine, but alas, I do not know how to work around the problem that you cannot assign dynamic variables in loops (say I named the variable for the extracted dates Returns.period, it would get overwritten every iteration of the loop, and I cant name something Returns.n, where n is the index for the loop)

"Thus, I tried to adjust by dynamically assigning each period to a column. However, I get the error: object 'Returns.period1' not found. And R is not like Java where you can simply declare a variable by typing say Returns.period1.

I do not know how to create a flexible empty matrix that would allow this code to work.

If anyone knows how to do this without a loop, that would be even better!

##Filtering by Period
n<-1
while(n<=19) {
        Returns.period<-lapply(((n-1)*125+1):((n+1)*125), function (i) which(Returns.filter$date_==x[i]))
        Returns.period<-unlist(Returns.period)
        Returns.period1[n,1:(length(Returns.period))]<-Returns.filter[Returns.period,]
        n<-n+1
        }"

Reply | Threaded
Open this post in threaded view
|

Odp: Extracting Elements By Date

PIKAL Petr
In reply to this post by Jeff08
Hi

You have got several suggestions. When I try your code I get only errors,
not list or any other objects.

> x<-unique(Returns.names$date_)
Error in unique(Returns.names$date_) : object 'Returns.names' not found
> n<-1
> while(n<=19) {
+     Returns.period<-lapply(((n-1)*125+1):((n+1)*125), function (i)
+  which(Returns.filter$date_==x[i]))
+     Returns.period<-unlist(Returns.period)
+
+
Returns.period1[n,1:(length(Returns.period))]<-Returns.filter[Returns.period,]
+     n<-n+1
+     }
Error in which(Returns.filter$date_ == x[i]) :
  object 'Returns.filter' not found
>

How do you expect to get reasonable help if you can not present what you
did and how result differ from what you want on some fake data which we
can copy to R.

[hidden email] napsal dne 09.06.2010 03:49:37:

>
> Dear R Gurus,
>
> Thanks for any help in advance!
>
> Date.frame: Returns.names
>
>        X       id ticker      date_ adjClose   totret RankStk
> 258060 258060 13645T10     CP 2001-06-29   18.125 1877.758
>
> My data frame is in the above format. I would like to filter by period,
per

what is filter??
Help page says

Description
Applies linear filtering to a univariate time series or to each series
separately of a multivariate time series.

> id (every 125 days) each consisting of 250 days, I.e. 1-250, 126-375,
etc.

<snip>

> Thus, I tried to adjust by dynamically assigning each period to a
column.
> However, I get the error: object 'Returns.period1' not found. And R is
not
> like Java where you can simply declare a variable by typing say
> Returns.period1.

Why do you expect that one language will behave the same way as another
language. If R was like Java what would be the reason to have 2 languages?

Try instead to comply posting guide and make a fake data which can be
directly used e.g. by dput, construct a result which is to be achieved and
present a code which can be copied to R and which gives other then
expected result.

After that we can quit guessing and start providing relevant help.

Regards
Petr

>
> I do not know how to create a flexible empty matrix that would allow
this
> code to work.
>
> If anyone knows how to do this without a loop, that would be even
better!
>
> ##Filtering by Period
> n<-1
> while(n<=19) {
>    Returns.period<-lapply(((n-1)*125+1):((n+1)*125), function (i)
> which(Returns.filter$date_==x[i]))
>    Returns.period<-unlist(Returns.period)
>
>
Returns.period1[n,1:(length(Returns.period))]<-Returns.filter[Returns.period,]

>    n<-n+1
>    }
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Extracting-
> Elements-By-Date-tp2248227p2248227.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Extracting Elements By Date

Gabor Grothendieck
In reply to this post by Jeff08
Try this and next time provide reproducible code and data as per
posting guide (see last line of every message).

First we create a sample data frame, DF.   Then we use the zoo time
series package to read it in and convert it from long form, DF, to
wide form, z, with one column per id.

Then we use split to split it into two day groups noting that %/%
signifies integer division. Since we have 4 successive days here this
gives two groups of two days each.

See ?read.zoo and the three zoo vignettes (pdf documents) that come
with the zoo package.  Also see ?split

> # sample data
> DF <- data.frame(Date = as.Date("2000-1-1") + 0:3, id = "A", value = 1:4)
> DF <- rbind(DF, transform(DF, id = "B"))
> DF
        Date id value
1 2000-01-01  A     1
2 2000-01-02  A     2
3 2000-01-03  A     3
4 2000-01-04  A     4
5 2000-01-01  B     1
6 2000-01-02  B     2
7 2000-01-03  B     3
8 2000-01-04  B     4
>
> library(zoo)
> # need devel version of read.zoo
> source("http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/zoo/R/read.zoo.R?revision=719&root=zoo")
> z <- read.zoo(DF, split = "id")
> z
           A B
2000-01-01 1 1
2000-01-02 2 2
2000-01-03 3 3
2000-01-04 4 4
> split(z, as.numeric(time(z) - start(z)) %/% 2)
$`0`
           A B
2000-01-01 1 1
2000-01-02 2 2

$`1`
           A B
2000-01-03 3 3
2000-01-04 4 4


On Tue, Jun 8, 2010 at 9:49 PM, Jeff08 <[hidden email]> wrote:

>
> Dear R Gurus,
>
> Thanks for any help in advance!
>
> Date.frame: Returns.names
>
>       X       id ticker      date_ adjClose   totret RankStk
> 258060 258060 13645T10     CP 2001-06-29   18.125 1877.758
>
> My data frame is in the above format. I would like to filter by period, per
> id (every 125 days) each consisting of 250 days, I.e. 1-250, 126-375, etc.
>
> One important thing to note is that not all ID's have the same number of
> dates.
>
> x<-unique(Returns.names$date_) gives me the ordered list of all the dates,
> so for each period i want it would be x[((n-1)*125+1):((n+1)*125)]
>
> when i tried to filter for just 1 period, it worked fine, but alas, I do not
> know how to work around the problem that you cannot assign dynamic variables
> in loops (say I named the variable for the extracted dates Returns.period,
> it would get overwritten every iteration of the loop, and I cant name
> something Returns.n, where n is the index for the loop)
>
> Thus, I tried to adjust by dynamically assigning each period to a column.
> However, I get the error: object 'Returns.period1' not found. And R is not
> like Java where you can simply declare a variable by typing say
> Returns.period1.
>
> I do not know how to create a flexible empty matrix that would allow this
> code to work.
>
> If anyone knows how to do this without a loop, that would be even better!
>
> ##Filtering by Period
> n<-1
> while(n<=19) {
>        Returns.period<-lapply(((n-1)*125+1):((n+1)*125), function (i)
> which(Returns.filter$date_==x[i]))
>        Returns.period<-unlist(Returns.period)
>
> Returns.period1[n,1:(length(Returns.period))]<-Returns.filter[Returns.period,]
>        n<-n+1
>        }
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Extracting-Elements-By-Date-tp2248227p2248227.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.