【问题标题】:How to aggregate data with respect to time level? [duplicate]如何根据时间级别聚合数据? [复制]
【发布时间】:2016-04-02 07:37:57
【问题描述】:

我一直在努力对有关月份和时间的数据进行平均。 我使用的数据是 6 个月(比如 1 月到 6 月),一列中间隔 15 分钟,第二列中时间段的值。 我使用下面提到的 cod 对从分钟间隔到每小时间隔的数据进行了平均:

library(xts)

data<-read.csv("C:/Users/naman.nagar/Downloads/JAVA &R/15_Minute_Site_ Avg.csv",header=TRUE,stringsAsFactors = FALSE)
data$Timestamp<-as.POSIXct(strptime(cognos_data$Timestamp,format="%Y-%m-%d %H:%M"))
data.xts<-xts(x=cognos_data$Wanamaker,cognos_data$Timestamp)
ep<-endpoints(data.xts,"hours")
period.apply(data.xts,ep,mean)

我使用上面的代码得到的数据是:

    2015-12-19 10:15:00 1602
    2015-12-19 11:15:00 1608
    2015-12-19 12:15:00 1590
    2015-12-19 13:15:00 1590
    2015-12-19 14:15:00 1344
    2015-12-19 15:15:00 1338
    2015-12-19 16:15:00 1338
    2015-12-19 17:15:00 1338
    2015-12-19 18:15:00 1338
    2015-12-19 19:15:00 1392
    2015-12-19 20:15:00 1368
    2015-12-19 21:15:00 1302
    2015-12-19 22:15:00 1302
    2015-12-19 23:15:00 1266
    2015-12-20 00:15:00 1248
    2015-12-20 01:15:00 1254
    2015-12-20 02:15:00 1218
    2015-12-20 03:15:00 1188

现在从这些数据中,我希望每月平均数据为:

    2015-12 10:00:00 1389
    2015-12 11:00:00 1390
    2015-12 12:00:00 1400
    2015-12 13:00:00 1396

意思是说我在每小时级别(例如 12:00:00)获得了整个 12 月的平均数据,以获得整个月份特定时间的平均值。

请帮忙。 提前致谢!

【问题讨论】:

    标签: r average


    【解决方案1】:

    我们可以在data.table 中做这样的事情:

    library(data.table)
    setDT(df)[, .(mean = mean(value)), by = .(year = format(Timestamp, "%Y"),
                                                month = format(Timestamp, "%m"), 
                                                hour = format(Timestamp, "%H"))]
    #   year month hour  mean
    #1: 2015    12   10  1602
    #2: 2015    12   11  1608
    #3: 2015    12   12  1590
    #4: 2015    12   13  1590
    #5: 2015    12   14  1344
    #6: 2015    12   15  1338
    

    【讨论】:

    • 非常感谢@mtoto。 :)
    【解决方案2】:

    这是使用dplyrlubridate 包的解决方案。 假设您有如下数据:

    library(dplyr)
    library(lubridate)
    
    # just to make it reproducible
    # also added a line at 10:00:00 so that we have at least once more than one value for hour 10
    
        data <- structure(list(timestamp = c("2015-12-19 10:00:00", "2015-12-19 10:15:00", 
    "2015-12-19 11:15:00", "2015-12-19 12:15:00", "2015-12-19 13:15:00", 
    "2015-12-19 14:15:00", "2015-12-19 15:15:00", "2015-12-19 16:15:00", 
    "2015-12-19 17:15:00", "2015-12-19 18:15:00", "2015-12-19 19:15:00", 
    "2015-12-19 20:15:00", "2015-12-19 21:15:00", "2015-12-19 22:15:00", 
    "2015-12-19 23:15:00", "2015-12-20 00:15:00", "2015-12-20 01:15:00", 
    "2015-12-20 02:15:00", "2015-12-20 03:15:00"), x = c(400, 1602, 
    1608, 1590, 1590, 1344, 1338, 1338, 1338, 1338, 1392, 1368, 1302, 
    1302, 1266, 1248, 1254, 1218, 1188)), .Names = c("timestamp", 
    "x"), row.names = c(NA, 19L), class = "data.frame")
    
    # let's have a look to it
    head(data)
    #                 timestamp    x
    # 1 2015-12-19 10:00:00  400
    # 2 2015-12-19 10:15:00 1602
    # 3 2015-12-19 11:15:00 1608
    # 4 2015-12-19 12:15:00 1590
    # 5 2015-12-19 13:15:00 1590
    # 6 2015-12-19 14:15:00 1344
    # etc.
    

    然后,我们使用下面的管道:i)创建新列 year_month(我想你会有多个)+ hours,ii)按 year_month 和 hours 分组,iii)总结每组的平均值(即给定月份的每小时):

    data %>% 
       mutate(year_month=paste(year(timestamp), month(timestamp), sep="-"),
              hour=hour(timestamp)) %>% 
       group_by(year_month, hour) %>% summarize(mean_x=mean(x))
    
        # year_month  hour mean_x
    # (chr) (int)  (dbl)
    # 1     2015-12     0   1248
    # 2     2015-12     1   1254
    # 3     2015-12     2   1218
    # 4     2015-12     3   1188
    # 5     2015-12    10   1001
    # 6     2015-12    11   1608
    

    记下 10 小时的值。

    这是你想要的吗?

    【讨论】:

    • 在某种程度上,但我确实有六个月的数据,我希望按小时计算每月数据的平均值。您的答案将对特定小时的 6 个月的完整数据集进行分组和平均,但是我需要每小时平均每个月的数据。由于数据为 6 个月,因此将产生 144 行(6 个月 *24 小时)。这有意义吗?
    • 当我意识到这一点时正在编辑我的答案 ;-)
    • 非常感谢文森特! :-)
    猜你喜欢
    • 2021-12-01
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2023-03-09
    • 2015-08-16
    • 2018-04-28
    相关资源
    最近更新 更多