[R] Extracting Elements By Date
Jeff08
jefferyding at gmail.com
Wed Jun 9 04:35:19 CEST 2010
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
}"
--
View this message in context: http://r.789695.n4.nabble.com/Extracting-Elements-By-Date-tp2248227p2248241.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list