Skip to content
Prev 8166 / 15274 Next

Iterating through subset of XTS object

On Tue, Jul 5, 2011 at 12:53 AM, Noah Silverman <noahsilverman at ucla.edu> wrote:
A reproducible example would have been nice (a "bars" object and code
without syntax errors).

You're going to have this issue with a lot of objects and for loops
because ?"for" says that seq (the part of a for loop after in) is
"[A]n expression evaluating to a vector (including a list and an
expression) or to a pairlist or 'NULL'".  xts objects are not strictly
vectors.
[1] FALSE

So it gets coerced to numeric.
[1] 3 4 5 6 8 7 6 5
The proper way is to loop over the indices of the object.

for(i in 1:NROW(bars['T10:00/T10:30',])) {
  bar <- bars[i,]
  # do stuff
}
Best,
--
Joshua Ulrich  |  FOSS Trading: www.fosstrading.com