Re: [R] Extracting Elements By Date

From: Jeff08 <>
Date: Tue, 08 Jun 2010 19:35:19 -0700 (PDT)

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
while(n<=19) {

        Returns.period<-lapply(((n-1)*125+1):((n+1)*125), function (i) which(Returns.filter$date_==x[i]))




View this message in context:
Sent from the R help mailing list archive at

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 09 Jun 2010 - 05:36:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Jun 2010 - 06:50:29 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive