创建一个由多个时间间隔组成的时间序列。

huangapple go评论92阅读模式
英文:

Create a time series by multiple time intervals

问题

我参考了下面的stackoverflow帖子,并发现可以通过以下代码创建一个30分钟间隔的时间序列。

帖子链接:https://stackoverflow.com/questions/35296749/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)

我的问题是,假设我想要从上午6点到9点创建一个30分钟的时间间隔,从上午9点到下午3点创建一个45分钟的时间间隔,我该如何实现?

一种方法是分别创建两个列表,然后将它们合并。然而,这不能保证在间隔时间不规则或不是5的倍数的情况下保持连续,比如16分钟、21分钟等。

英文:

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: https://stackoverflow.com/questions/35296749/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

答案1

得分: 1

我不清楚为什么你认为不能简单地连接系列,但这里有一个15分钟系列与一个16分钟系列连接在一起。我们使用unique来排除重复的时间。

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")))
英文:

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 &lt;- paste(&quot;2023-01-02&quot;, c(&quot;06:00:00&quot;, &quot;09:00:00&quot;, &quot;11:00:00&quot;))
p &lt;- as.POSIXct(p0, tz = &quot;UTC&quot;)
unique(c(seq(p[1], p[2], by = &quot;15 min&quot;), seq(p[2], p[3], by = &quot;16 min&quot;)))

答案2

得分: 1

如何这样做:

cont <- function(start, mid, end, int1, int2) {
  seq1 <- seq(as.POSIXct(start, tz = "UTC"),
    as.POSIXct(mid, tz = "UTC"),
    by = paste(int1, "min")) # 使用传递的整数参数和单位(min)创建字符串
  seq2 <- seq(seq1[length(seq1)], # 使用第一个序列的最后一个元素
    as.POSIXct(end, tz = "UTC"),
    by = paste(int2, "min"))
  return(c(seq1, seq2[-1])) # 删除第二个序列的第一个元素以避免重复,并返回
}

我假设你说的连续性意味着你不想要除了两个指定的间隔之外的间隔。这个函数首先创建初始序列,然后取最后一个元素并将其用作第二个序列的起始点,以确保连续性。例如

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

应该返回

[1] "2023-01-01 08:00:00 UTC" "2023-01-01 08:59:00 UTC" "2023-01-01 09:15:00 UTC"
英文:

How about something like this:

cont &lt;- function(start, mid, end, int1, int2) {
  seq1 &lt;- seq(as.POSIXct(start, tz = &quot;UTC&quot;),
    as.POSIXct(mid, tz = &quot;UTC&quot;),
    by = paste(int1, &quot;min&quot;)) #creates a string from the integer passed as an argument and the unit (min)
  seq2 &lt;- seq(seq1[length(seq1)], #uses last element of first sequence
    as.POSIXct(end, tz = &quot;UTC&quot;),
    by = paste(int2, &quot;min&quot;))
  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(&quot;2023-01-01 08:00&quot;, &quot;2023-01-01 09:00&quot;, &quot;2023-01-01 09:30&quot;, 59, 16)

Should return

[1] &quot;2023-01-01 08:00:00 UTC&quot; &quot;2023-01-01 08:59:00 UTC&quot; &quot;2023-01-01 09:15:00 UTC&quot;

huangapple
  • 本文由 发表于 2023年7月31日 23:15:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定