[R] Extracting Elements By Date

From: Jeff08 <jefferyding_at_gmail.com>
Date: Tue, 08 Jun 2010 18:49:37 -0700 (PDT)

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: http://r.789695.n4.nabble.com/Extracting-Elements-By-Date-tp2248227p2248227.html
Sent from the R help mailing list archive at Nabble.com.

R-help_at_r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 09 Jun 2010 - 05:33:29 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 - 11:00:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive