[R] Ideas for tricky subset of XTS object

Noah Silverman noahsilverman at ucla.edu
Tue Aug 23 22:41:50 CEST 2011


Hi,

I have a very large XTS object.  It is about 600,000 entries over 1 year time.

I would like to subset a specific piece, by "number of days" not a specific date.

The way I do it now is awkward.  Would love to find a way to do this easier to generate a new object just containing the days I want.

---------------------------------------
dayEnds <- endpoints(dataset, on="days")
datasetEnd <- max(dayEnds)
for( d in 1:ndays(dataset)){
	day <- dataset[ (dayEnds[d]+1):dayEnds[d+1], ]
	
	daysToEnd <- round(as.numeric( index(dataset[datasetEnd]) - index(tail(day, n=1)) , unit="days"  ))
	if(daysToEnd > 30|| daysToEnd < 10){
		next;
	}
	
	# Will only get here if the days until the end are between 10 and 30
	#  Do the work here
}
-------------------------------------------


The above works but:
	1) It is slow and awkward
	2) I never wind up with a single clean dataset of just the days I want.  

Any ideas?

Thanks!

--
Noah Silverman
UCLA Department of Statistics
8117 Math Sciences Building
Los Angeles, CA 90095



More information about the R-help mailing list