[R-SIG-Finance] Iterating through subset of XTS object

Jeff Ryan jeff.a.ryan at gmail.com
Tue Jul 5 15:10:07 CEST 2011



Jeffrey Ryan    |    Founder    |    jeffrey.ryan at lemnica.com

www.lemnica.com

On Jul 5, 2011, at 12:53 AM, Noah Silverman <noahsilverman at ucla.edu> wrote:

> Hi,
> 
> I have an xts object with 1 minute bars of trading data.
> 
> I want to select a subset, then iterate through each bar.  The "obvious" solution doesn't work:
> 
> For each bar in the loop, I want to access all the fields (open, close, volume, etc.)
> 
> 
> I tried the following, but it doesn't work:
> 
> ----------------------------------------------
> for( one in bars['T10:00/T10"30', ]){
>    print(one$open)
>    print(one$close)
> }
> ---------------------------------------------
> 

Your solution isn't really obvious enough to R :-)

Try a very basic loop like

for(i in 1:nrow(xts_obj)) {
  xts_obj[i]
}

And make sure you subset by time first to avoid repeating that. 

You could also use apply

In general this is more an R-help question, as it has nothing to do with finance. 


> Note: I DO need the loop for the code I'm writing.  
> 

You might be surprised that this is likely not true ... But you probably expected someone to point that out. 
> Can someone suggest the proper way to do this?
> 

Best,
Jeff
> Thanks!
> 
> --
> Noah Silverman
> UCLA Department of Statistics
> 8117 Math Sciences Building
> Los Angeles, CA 90095
> 
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.



More information about the R-SIG-Finance mailing list