How to create a vector with repeating dates?

3

I need to create a vector with sequential dates between 01/01/2013 and 05/31/2018, but with repetitions, as shown below, associated with time.

01/01/2013 0:00 01/01/2013 1:00 01/01/2013 4:00 01/01/2013 8:00 01/01/2013 12:00 01/01/2013 18:00 02/01/2013 0:00 02/01/2013 1:00 02/01/2013 4:00 02/01/2013 8:00 02/01/2013 12:00 02/01/2013 18:00 03/01/2013 0:00 03/01/2013 1:00 03/01/2013 4:00 03/01/2013 8:00 03/01/2013 12:00 03/01/2013 18:00

How can I do this automatically?

    
asked by anonymous 09.05.2018 / 15:21

2 answers

4

Despite your answer to your question, I will also respond. This is for two reasons.

  • In the question data each day is repeated 6 times and with by = 0.5 only repeats two.
  • There are also the missing hours in your reply.
  • First, I start by creating a vector of dates. I've modified it a bit with% of% to make the seq/as.Date increment more clear. The second instruction repeats every day such 6 times. Note that by = "days" is a base R function, so I called this vector data .

    Data <- seq(as.Date('2013/01/01', "%Y/%m/%d"), as.Date('2018/05/31', "%Y/%m/%d"), by = "days")
    Data <- rep(Data, each = 6)
    

    Second, I create a class vector Data with the hours. There is also a "character" function but in this case it can not be used because the hour increments are not constant, the question time differences are seq.POSIXt .

    Hora <- c("0:00", "1:00", "4:00", "8:00", "12:00", "18:00")
    Hora <- rep(Hora, length.out = length(Data))
    

    Finally, I create a 1 3 4 4 6 with the two vectors in the same column.

    dados <- data.frame(Data = as.POSIXct(paste(Data, Hora), format = "%Y-%m-%d %H:%M"))
    
    head(dados, n = 20)
    #                  Data
    #1  2013-01-01 00:00:00
    #2  2013-01-01 01:00:00
    #3  2013-01-01 04:00:00
    #4  2013-01-01 08:00:00
    #5  2013-01-01 12:00:00
    #6  2013-01-01 18:00:00
    #7  2013-01-02 00:00:00
    #8  2013-01-02 01:00:00
    #9  2013-01-02 04:00:00
    #10 2013-01-02 08:00:00
    #11 2013-01-02 12:00:00
    #12 2013-01-02 18:00:00
    #13 2013-01-03 00:00:00
    #14 2013-01-03 01:00:00
    #15 2013-01-03 04:00:00
    #16 2013-01-03 08:00:00
    #17 2013-01-03 12:00:00
    #18 2013-01-03 18:00:00
    #19 2013-01-04 00:00:00
    #20 2013-01-04 01:00:00
    
        
    09.05.2018 / 18:03
    3

    The vector can be obtained from the function seq(from, to, by= ) , indicating the endpoint of the sequence, designated by by, as shown below.

    > data <- seq(as.Date('2013/01/01', "%Y/%m/%d"), as.Date('2018/05/31', "%Y/%m/%d"), 1)   > data [1] "2013-01-01" "2013-01-02" "2013-01-03" "2013-01-04" "2013-01-05"...

    Changing the number 1 at the end of the function, which allows a daily interval, it is possible to get subdial values.

    > data <- seq(as.Date('2013/01/01', "%Y/%m/%d"), as.Date('2018/05/31', "%Y/%m/%d"), 0.5)   > data [1] "2013-01-01" "2013-01-01" "2013-01-02" "2013-01-02" "2013-01-03" "2013-01-03"...

        
    09.05.2018 / 17:31