Hello, I am trying to do a loop with dates, but when I try to use the index is not a date. Fcorte <- as.Date('2008/11/30',format = "%Y/%m/%d") fini <- Fcorte + 1 ffin <- seq(fini,by='months',length=2)[2] - 1 for (i in seq(fini,to = ffin, by='days')) print (weekdays(i)) # i doesn't a date How can I do a loop with dates and get the index of each date? are there a method to convert the index i to date? Thanks in advance. [[alternative HTML version deleted]]
Try this: weekdays(seq(fini,to = ffin, by='days')) or in a loop: sapply(as.character(seq(fini,to = ffin, by='days')), function(d)weekdays(as.Date(d))) On Fri, Dec 12, 2008 at 4:55 PM, Fernando Bizuet <fbizuet@gmail.com> wrote:> Hello, > > I am trying to do a loop with dates, but when I try to use the index is not > a date. > > Fcorte <- as.Date('2008/11/30',format = "%Y/%m/%d") > fini <- Fcorte + 1 > ffin <- seq(fini,by='months',length=2)[2] - 1 > > for (i in seq(fini,to = ffin, by='days')) > print (weekdays(i)) # i doesn't a date > > How can I do a loop with dates and get the index of each date? are there a > method to convert the index i to date? > > > Thanks in advance. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
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:> Hello, > > I am trying to do a loop with dates, but when I try to use the index is not > a date. > > Fcorte <- as.Date('2008/11/30',format = "%Y/%m/%d") > fini <- Fcorte + 1 > ffin <- seq(fini,by='months',length=2)[2] - 1 > > for (i in seq(fini,to = ffin, by='days')) > print (weekdays(i)) # i doesn't a date > > How can I do a loop with dates and get the index of each date? are there a > method to convert the index i to date? > > > Thanks in advance. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Fernando Bizuet > Sent: Friday, December 12, 2008 12:55 PM > To: r-help at r-project.org > Subject: [R] loop with dates > > Hello, > > I am trying to do a loop with dates, but when I try to use > the index is not > a date.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> > Fcorte <- as.Date('2008/11/30',format = "%Y/%m/%d") > fini <- Fcorte + 1 > ffin <- seq(fini,by='months',length=2)[2] - 1 > > for (i in seq(fini,to = ffin, by='days')) > print (weekdays(i)) # i doesn't a datetypeof(ffin) [1] "double" As your index is no longer of class 'Date', you will get Error in UseMethod("weekdays") : no applicable method for "weekdays> > How can I do a loop with dates and get the index of each > date? are there a > method to convert the index i to date?Here's one way dd <- seq(fini,to = ffin, by='days') for (i in seq_along(dd)) print(dd[i])> > > Thanks in advance. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >