Re: [R] Loop With Dates
Just to add one more option (which is best probably depends on if all the same dates are together in adjacent rows, if an earlier date can come later in the data frame, and other things): df$count <- cumsum(!duplicated(df$Date)) Skill a cumsum of logicals, just a different way of getting the logicals. On Sun, Sep 22, 2019 at 5:20 AM Richard O'Keefe wrote: > > Is this what you're after? > > > df <- data.frame( > + Date = as.Date(c("2018-03-29", "2018-03-29", "2018-03-29", > + "2018-03-30", "2018-03-30", "2018- ..." ... > [TRUNCATED] > > > df$count <- cumsum(c(TRUE, diff(df$Date) > 0)) > > df > Date count > 1 2018-03-29 1 > 2 2018-03-29 1 > 3 2018-03-29 1 > 4 2018-03-30 2 > 5 2018-03-30 2 > 6 2018-03-30 2 > 7 2018-03-31 3 > 8 2018-03-31 3 > 9 2018-03-31 3 > > No extra libraries needed. Whenever you want a vector that counts something, > cumsum of a logical vector is a good thing to try. > > On Sat, 21 Sep 2019 at 05:47, Phillip Heinrich wrote: > > > > With the data snippet below I’m trying to increment the “count” vector by > > one each time the date changes. > > > > Date count > > 1 2018-03-29 1 > > 2 2018-03-29 1 > > 3 2018-03-29 1 > > 81 2018-03-30 1 > > 82 2018-03-30 1 > > 83 2018-03-30 1 > > 165 2018-03-31 1 > > 166 2018-03-31 1 > > 167 2018-03-31 1 > > > > > > > > > > > > > > > I can get count to change when the date changes with the following code: > > > > test2 <- transform(test2, > > + count = ifelse(Date == lag(Date,1),count,count+1)) > > > test2 > > Date count > > 1 2018-03-29NA > > 2 2018-03-29 1 > > 3 2018-03-29 1 > > 81 2018-03-30 2 > > 82 2018-03-30 1 > > 83 2018-03-30 1 > > 165 2018-03-31 2 > > 166 2018-03-31 1 > > 167 2018-03-31 1 > > > > > > > > > > > > > > > > ...but I want all three March 30 rows to have a count of 2 and the March 31 > > rows to be equal to 3. Any suggestions? > > > > Thanks. > > [[alternative HTML version deleted]] > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Loop With Dates
Is this what you're after? > df <- data.frame( + Date = as.Date(c("2018-03-29", "2018-03-29", "2018-03-29", + "2018-03-30", "2018-03-30", "2018- ..." ... [TRUNCATED] > df$count <- cumsum(c(TRUE, diff(df$Date) > 0)) > df Date count 1 2018-03-29 1 2 2018-03-29 1 3 2018-03-29 1 4 2018-03-30 2 5 2018-03-30 2 6 2018-03-30 2 7 2018-03-31 3 8 2018-03-31 3 9 2018-03-31 3 No extra libraries needed. Whenever you want a vector that counts something, cumsum of a logical vector is a good thing to try. On Sat, 21 Sep 2019 at 05:47, Phillip Heinrich wrote: > > With the data snippet below I’m trying to increment the “count” vector by one > each time the date changes. > > Date count > 1 2018-03-29 1 > 2 2018-03-29 1 > 3 2018-03-29 1 > 81 2018-03-30 1 > 82 2018-03-30 1 > 83 2018-03-30 1 > 165 2018-03-31 1 > 166 2018-03-31 1 > 167 2018-03-31 1 > > > > > > > > I can get count to change when the date changes with the following code: > > test2 <- transform(test2, > + count = ifelse(Date == lag(Date,1),count,count+1)) > > test2 > Date count > 1 2018-03-29NA > 2 2018-03-29 1 > 3 2018-03-29 1 > 81 2018-03-30 2 > 82 2018-03-30 1 > 83 2018-03-30 1 > 165 2018-03-31 2 > 166 2018-03-31 1 > 167 2018-03-31 1 > > > > > > > > ...but I want all three March 30 rows to have a count of 2 and the March 31 > rows to be equal to 3. Any suggestions? > > Thanks. > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Loop With Dates
Hi Phillip, While I really like Ana's solution, this might also help: phdf<-read.table(text="Date count 2018-03-29 1 2018-03-29 1 2018-03-29 1 2018-03-30 1 2018-03-30 1 2018-03-30 1 2018-03-31 1 2018-03-31 1 2018-03-31 1", header=TRUE,stringsAsFactors=FALSE) phdf$Date<-as.Date(phdf$Date,"%Y-%m-%d") incflag<-diff(phdf$Date)>0 phdf$count<-c(NA,cumsum(incflag)+1) Jim On Sat, Sep 21, 2019 at 3:47 AM Phillip Heinrich wrote: > > With the data snippet below I’m trying to increment the “count” vector by one > each time the date changes. > > Date count > 1 2018-03-29 1 > 2 2018-03-29 1 > 3 2018-03-29 1 > 81 2018-03-30 1 > 82 2018-03-30 1 > 83 2018-03-30 1 > 165 2018-03-31 1 > 166 2018-03-31 1 > 167 2018-03-31 1 > > > > > > > > I can get count to change when the date changes with the following code: > > test2 <- transform(test2, > + count = ifelse(Date == lag(Date,1),count,count+1)) > > test2 > Date count > 1 2018-03-29NA > 2 2018-03-29 1 > 3 2018-03-29 1 > 81 2018-03-30 2 > 82 2018-03-30 1 > 83 2018-03-30 1 > 165 2018-03-31 2 > 166 2018-03-31 1 > 167 2018-03-31 1 > > > > > > > > ...but I want all three March 30 rows to have a count of 2 and the March 31 > rows to be equal to 3. Any suggestions? > > Thanks. > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Loop With Dates
Hello, Maybe I am not understanding but isn't this what you have asked in your previous question and my 2nd post (adapted) does? If not, where does it fail? Hope this helps, Rui Barradas Às 18:46 de 20/09/19, Phillip Heinrich escreveu: With the data snippet below I’m trying to increment the “count” vector by one each time the date changes. Date count 1 2018-03-29 1 2 2018-03-29 1 3 2018-03-29 1 81 2018-03-30 1 82 2018-03-30 1 83 2018-03-30 1 165 2018-03-31 1 166 2018-03-31 1 167 2018-03-31 1 > I can get count to change when the date changes with the following code: test2 <- transform(test2, + count = ifelse(Date == lag(Date,1),count,count+1)) test2 Date count 1 2018-03-29NA 2 2018-03-29 1 3 2018-03-29 1 81 2018-03-30 2 82 2018-03-30 1 83 2018-03-30 1 165 2018-03-31 2 166 2018-03-31 1 167 2018-03-31 1 ...but I want all three March 30 rows to have a count of 2 and the March 31 rows to be equal to 3. Any suggestions? Thanks. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Loop With Dates
Hi Phillip, This can be done in several ways as most things in programming. Here is one posible solution: dates <- c("2018-03-29", "2018-03-29", "2018-03-29", "2018-03-30", "2018-03-30", "2018-03-30", "2018-03-31", "2018-03-31", "2018-03-31") dates <- as.data.frame(as.Date(dates)) library(zoo) dates <- zoo(dates) colnames(dates) <- "dates" dates$lag <- lag(dates, -1, na.pad = TRUE) dates[1, 2] <- dates[1, 1] dates$count <- cumsum(!(dates$dates == dates$lag)) + 1 dates$lag <- NULL > dates dates.object count 1 2018-03-29 1 2 2018-03-29 1 3 2018-03-29 1 4 2018-03-30 2 5 2018-03-30 2 6 2018-03-30 2 7 2018-03-31 3 8 2018-03-31 3 9 2018-03-31 3 De: Phillip Heinrich Enviado: viernes, 20 de septiembre de 2019 19:47 Para: r-help Asunto: [R] Loop With Dates With the data snippet below I’m trying to increment the “count” vector by one each time the date changes. Date count 1 2018-03-29 1 2 2018-03-29 1 3 2018-03-29 1 81 2018-03-30 1 82 2018-03-30 1 83 2018-03-30 1 165 2018-03-31 1 166 2018-03-31 1 167 2018-03-31 1 > I can get count to change when the date changes with the following code: test2 <- transform(test2, + count = ifelse(Date == lag(Date,1),count,count+1)) > test2 Date count 1 2018-03-29NA 2 2018-03-29 1 3 2018-03-29 1 81 2018-03-30 2 82 2018-03-30 1 83 2018-03-30 1 165 2018-03-31 2 166 2018-03-31 1 167 2018-03-31 1 ...but I want all three March 30 rows to have a count of 2 and the March 31 rows to be equal to 3. Any suggestions? Thanks. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] Loop With Dates
With the data snippet below I’m trying to increment the “count” vector by one each time the date changes. Date count 1 2018-03-29 1 2 2018-03-29 1 3 2018-03-29 1 81 2018-03-30 1 82 2018-03-30 1 83 2018-03-30 1 165 2018-03-31 1 166 2018-03-31 1 167 2018-03-31 1 > I can get count to change when the date changes with the following code: test2 <- transform(test2, + count = ifelse(Date == lag(Date,1),count,count+1)) > test2 Date count 1 2018-03-29NA 2 2018-03-29 1 3 2018-03-29 1 81 2018-03-30 2 82 2018-03-30 1 83 2018-03-30 1 165 2018-03-31 2 166 2018-03-31 1 167 2018-03-31 1 ...but I want all three March 30 rows to have a count of 2 and the March 31 rows to be equal to 3. Any suggestions? Thanks. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] loop with dates
> -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Fernando Bizuet > Sent: Friday, December 12, 2008 12:55 PM > To: r-help@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 date 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 > > 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@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. > __ 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.
Re: [R] loop with dates
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 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. > __ 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.
Re: [R] loop with dates
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 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]] __ 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.
[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. 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.