0

I referred to the post in stackoverflow below and found that creating a time-series by 30 miniute interval can be acheived by the code below.

Post: Create a time series by 30 minute intervals

ts <- seq(as.POSIXct("2023-01-01", tz = "UTC"),
    as.POSIXct("2023-01-02", tz = "UTC"),
    by = "30 min")
head(ts)

My question is lets say I want to create a time interval of 30 minutes from 6 to 9am and 45 minutes for 9 to 3am how can I achieve this?

One way could be to create two lists separately and join. However, this does not ensure continuity when interval times are irregular are not multiples of 5 like 16 minutes, 21 minutes, ..so on

vp_050
  • 583
  • 2
  • 4
  • 16
  • 2
    What have you tried? you can do two `seq` and `c`oncatenate them. – Maël Jul 31 '23 at 15:18
  • I initially thought of this solution but it doesn't guarantee continuity when tine intervals are irregular like let's say 21 minutes or 16 minutes. – vp_050 Jul 31 '23 at 15:23

2 Answers2

1

I am unclear on why you believe you can't just concatenate series but here is a 15 minute series concatenated with a 16 minute series. We use unique to omit the duplicated times.

p0 <- paste("2023-01-02", c("06:00:00", "09:00:00", "11:00:00"))
p <- as.POSIXct(p0, tz = "UTC")
unique(c(seq(p[1], p[2], by = "15 min"), seq(p[2], p[3], by = "16 min")))
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
1

How about something like this:

cont <- function(start, mid, end, int1, int2) {
  seq1 <- seq(as.POSIXct(start, tz = "UTC"),
    as.POSIXct(mid, tz = "UTC"),
    by = paste(int1, "min")) #creates a string from the integer passed as an argument and the unit (min)
  seq2 <- seq(seq1[length(seq1)], #uses last element of first sequence
    as.POSIXct(end, tz = "UTC"),
    by = paste(int2, "min"))
  return(c(seq1, seq2[-1])) #drops first element of second sequence to avoid a duplicate and returns
  }

I am assuming that by continuity you mean you do not want intervals other then the two specified. This function first creates the initial sequence, then it takes the last element and uses it as a start of the second sequence ensuring continuity. Fore instance

cont("2023-01-01 08:00", "2023-01-01 09:00", "2023-01-01 09:30", 59, 16)

Should return

[1] "2023-01-01 08:00:00 UTC" "2023-01-01 08:59:00 UTC" "2023-01-01 09:15:00 UTC"
Adam
  • 95
  • 6