[R] Loop With Dates

Greg Snow 538280 @end|ng |rom gm@||@com
Tue Sep 24 19:55:12 CEST 2019


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 <raoknz using gmail.com> 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 <herd_dog using cox.net> 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-29    NA
> > 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 using 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 using 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.
538280 using gmail.com



More information about the R-help mailing list