How to create an array with repeating dates?

Asked

Viewed 237 times

3

I need to create a vector with sequential dates between 01/01/2013 and 05/31/2018, but with repetitions, as an example 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?

  • Ever tried to do something? Share with us your code so far.

  • I only know how to create the date vector in this way data <- seq(as.Date('2013/01/01', "%Y/%m/%d"), as.Date('2018/05/31', "%Y/%m/%d"), 1). But I don’t know how to determine repetitions.

  • 2

    I got it. There was no attempt that I can change the 1 of the end of the function to fractions of day. Thank you!

  • 1

    @Andreiaalmeida you can answer your own question! It is a good practice to leave the answer so that others also find in future research.

2 answers

4

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

  1. In the question data each day is repeated 6 times and with by = 0.5 only repeats two.
  2. There are also the missing hours in your reply.

First of all, I start by creating a vector of dates. I modified your seq/as.Date to make the increment clearer by = "days". The second instruction repeats each day such 6 times.
Note that data 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 "character" with the hours. There is also a function seq.POSIXt but in this case can not be used because the increments of the hours are not constant, the differences of hours of the question data are 1 3 4 4 6.

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 data.frame 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

3

The vector can be obtained from the function seq(from, to, by= ), indicating the end point of the sequence, designated by by, as an example 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"...

By changing the number 1 at the end of the function, which allows a daily interval, it is possible to obtain subdiary 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"...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.