Skip to content

loop with dates

4 messages · Fernando Bizuet, Henrique Dallazuanna, Gabor Grothendieck +1 more

#
Try iterating over the index rather than the value of
each component:

s <- seq(fini,to = ffin, by='days')
for (i in seq_along(s)) print(s[[i]])
On Fri, Dec 12, 2008 at 1:55 PM, Fernando Bizuet <fbizuet at gmail.com> wrote:
#
See ?"for". That help page mentions the following which can clarify what
to expect 

"... The variable 'var' has the same type as 'seq' ..."

Small illustration,

fac  <- gl(5,1,labels=letters[1:5])
fac
[1] a b c d e
Levels: a b c d e

typeof(fac)
[1] "integer"

for(i in fac) print(i)
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
typeof(ffin)
[1] "double"

As your index is no longer of class 'Date', you will get
Error in UseMethod("weekdays") : no applicable method for "weekdays
Here's one way 
dd <- seq(fini,to = ffin, by='days')
for (i in seq_along(dd)) print(dd[i])