【问题标题】:Set the start point for time intervals in R在 R 中设置时间间隔的起点
【发布时间】:2018-01-29 16:25:54
【问题描述】:

我有不同的数据集,格式如下

Time                    Value1 Value2 ....
11/04/2015  15:12:22    1      2      ....
11/04/2015  15:13:46    1      2      ....

我想以 15 分钟为间隔对它们进行分组。我可以用下面的代码做到这一点

data$time = cut(data$time, breaks = "15 min")
data.grouped <- aggregate(data[,c(-1)], by = list(time = datos$time), median)

问题是输出中的时间字段有以下值

12/04/2015 16:12
12/04/2015 16:27
12/04/2015 16:42
12/04/2015 16:57

我希望时间是 :00 :15 :30 或 :45。有什么方法可以强制间隔像这样或不同的方法来合并允许它的数据?

来自 dput 的样本数据:

structure(list(time = structure(list(sec = c(49, 5, 21, 37, 54, 
10, 38), min = c(12L, 13L, 13L, 13L, 13L, 14L, 22L), hour = c(15L, 
15L, 15L, 15L, 15L, 15L, 16L), mday = c(11L, 11L, 11L, 11L, 11L, 
11L, 12L), mon = c(3L, 3L, 3L, 3L, 3L, 3L, 3L), year = c(116L, 
116L, 116L, 116L, 116L, 116L, 116L), wday = c(1L, 1L, 1L, 1L, 
1L, 1L, 2L), yday = c(101L, 101L, 101L, 101L, 101L, 101L, 102L
), isdst = c(1L, 1L, 1L, 1L, 1L, 1L, 1L), zone = c("CEST", "CEST", 
"CEST", "CEST", "CEST", "CEST", "CEST"), gmtoff = c(NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_)), .Names = c("sec", "min", "hour", "mday", "mon", 
"year", "wday", "yday", "isdst", "zone", "gmtoff"), class = c("POSIXlt", 
"POSIXt")), value1 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("time", 
"value1"), row.names = c(NA, -7L), class = "data.frame")

【问题讨论】:

  • 您能否与dput() 共享示例数据,以便轻松复制/粘贴?你展示它的方式,它看起来不像是一个日期时间类,但你使用它的方式必须是......dput() 也会消除任何混淆。
  • 也许你只是想要lubridate::floor_date(data$time, unit = "15 min")? (也不确定您的数据中Time 的大小写,或者您的数据框名称......在问题中不一致。)
  • 当然,我应该将它添加到问题中还是您推荐其他网站发布它?
  • 将其添加到问题中。只需将其设为相关子集,6 行 2 列就足够了。
  • 我从 dput 添加了示例数据

标签: r time format


【解决方案1】:

从您的dput 开始,称它为df,首先我们会将您的因子转换为POSIXct 类,然后我们会将floor 转换为最接近下方15 分钟的值。 (如果您想要最近的 15 分钟,请使用 round 而不是 floor):

df$time = as.POSIXct(df$time)
df$time15 = lubridate::floor_date(df$time, unit = "15 min")
df
#                  time value1              time15
# 1 2016-04-11 15:12:49      0 2016-04-11 15:00:00
# 2 2016-04-11 15:13:05      0 2016-04-11 15:00:00
# 3 2016-04-11 15:13:21      0 2016-04-11 15:00:00
# 4 2016-04-11 15:13:37      0 2016-04-11 15:00:00
# 5 2016-04-11 15:13:54      0 2016-04-11 15:00:00
# 6 2016-04-11 15:14:10      0 2016-04-11 15:00:00
# 7 2016-04-12 16:22:38      0 2016-04-12 16:15:00

然后您可以使用time15 列作为分组器进行聚合。

【讨论】:

  • floor_date 正是我所需要的。非常感谢:)
【解决方案2】:

我提供了一个示例,您可以使用您的数据框进行复制。首先,我以 5 分钟间隔创建一个虚拟时间序列 (ts) as.POSIXct,然后使用 dplyr 按 15 分钟间隔对它们进行分组。

ts <- seq.POSIXt(as.POSIXct("2017-01-01", tz = "UTC"),
                 as.POSIXct("2017-02-01", tz = "UTC"),
                 by = "5 min")
ts <- as.data.frame(ts)
library(dplyr)
ts %>%
   group_by(interval = cut(ts, breaks = "15 min")) %>%
   summarise(count= n())

输出

# A tibble: 2,977 x 2
   interval            sumvalue
   <fct>                  <int>
 1 2017-01-01 00:00:00        3
 2 2017-01-01 00:15:00        3
 3 2017-01-01 00:30:00        3
 4 2017-01-01 00:45:00        3
 5 2017-01-01 01:00:00        3
 6 2017-01-01 01:15:00        3
 7 2017-01-01 01:30:00        3
 8 2017-01-01 01:45:00        3
 9 2017-01-01 02:00:00        3
10 2017-01-01 02:15:00        3
# ... with 2,967 more rows

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-21
    • 2022-12-07
    • 1970-01-01
    • 2021-02-05
    • 2023-01-12
    • 2017-12-03
    • 2018-06-26
    • 2012-05-28
    相关资源
    最近更新 更多