Loop With Dates

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Loop With Dates

Phillip Heinrich
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loop With Dates

Ana PGG
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-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]]

______________________________________________
[hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loop With Dates

Rui Barradas
In reply to this post by Phillip Heinrich
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-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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loop With Dates

Jim Lemon-4
In reply to this post by Phillip Heinrich
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loop With Dates

Richard O'Keefe-2
In reply to this post by Phillip Heinrich
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: Loop With Dates

Rolf Turner

On 22/09/19 11:19 PM, Richard O'Keefe wrote:

<SNIP>

> Whenever you want a vector that counts something,
> cumsum of a logical vector is a good thing to try.

<SNIP>

Fortune nomination.

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loop With Dates

glsnow
In reply to this post by Richard O'Keefe-2
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 <[hidden email]> 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 <[hidden email]> 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]]
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
[hidden email]

______________________________________________
[hidden email] 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.