dplyr code" df%>%group_by(date = cut(date,breaks =" 1小时"))"不再产生预期的结果?

时间:2017-09-12 20:31:19

标签: r datetime group-by dplyr summarize

我一直在使用以下dplyr代码从1分钟的时间序列数据生成每小时平均值。该代码已经工作了几个月,但最近产生了一些有问题的结果。使用以下任何功能更改了某些内容:group_by()cut()summarise()

df <- structure(list(date = structure(c(1505187300, 1505187360, 1505187420, 1505187480, 1505187540, 1505187600, 1505187660, 1505187720, 1505201580, 1505201640), class = c("POSIXct", "POSIXt"), tzone = "UTC"), co = c(0.149,0.149,0.149, 0.106, 0.149, 0.149, 0.192, 0.149, 0.149, 0.149), co2 = c(544L, 545L, 544L, 543L, 546L, 546L, 548L, 547L, 549L, 554L), VOC = c(22.55, 22.55, 22.8198, 23.2602, 22.9501, 23.2154, 23.4262, 23.0231, 23.0525, 22.7911), RH = c(77.02, 76.9, 77.2, 76.6, 76.99, 76.83, 77.13, 77.81, 77.48, 77.1), ugm3 = c(12.862, 13.408, 14.188, 12.342, 13.278, 12.81, 10.834, 13.018, 12.992, 12.498), temp = c(62.06, 62.02, 62.02, 61.98, 61.94, 61.9, 61.86, 61.78, 61.8, 61.8)), .Names = c("date", "co", "co2", "VOC", "RH", "ugm3", "temp"), row.names = c(NA, 10L), class = "data.frame")

new_df <- df %>% 
    group_by(date = cut(date, breaks = "1 hour")) %>%
    summarize(co = mean(co), co2 = mean(co2), VOC = mean(VOC), RH = mean(RH), ugm3 = mean(ugm3), temp = mean(temp))

new_df

预期输出:

expected_output <- structure(list(date = structure(c(1L, 5L), .Label = c("2017-09-12 03:00:00", "2017-09-12 04:00:00", "2017-09-12 05:00:00", "2017-09-12 06:00:00", "2017-09-12 07:00:00"), class = "factor"), co = c(0.149, 0.149), co2 = c(545.375, 551.5), VOC = c(22.97435, 22.9218), RH = c(77.06, 77.29), ugm3 = c(12.8425, 12.745), temp = c(61.945, 61.8)), class = c("tbl_df", "tbl", "data.frame"), .Names = c("date", "co", "co2", "VOC", "RH", "ugm3", "temp"), row.names = c(NA, -2L))

实际输出:

actual_output <- structure(list(co = 0.149, co2 = 546.6, VOC = 22.96384, RH = 77.106, ugm3 = 12.823, temp = 61.916), .Names = c("co", "co2", "VOC", "RH", "ugm3", "temp"), class = "data.frame", row.names = c(NA, -1L))

在本周之前,此代码会生成一个新的df,其中包含两个观察值,一个用于03:00:00小时,另一个用于07:00:00小时。虽然group_by()函数似乎正在正确分配新的每小时时间戳,但summarize()函数的行为不正常。任何见解都表示赞赏。谢谢!

如果有更强大的替代方案可以将时间序列数据汇总到特定的时间间隔内,那我就是所有的耳朵!

1 个答案:

答案 0 :(得分:3)

您在plyr之后加载了dplyr

library(dplyr)
# ...
library(plyr)
# ------------------------------------------------------------------------------# -------------------------------------------
# 
# Attachement du package : ‘plyr’
# 
# The following objects are masked from ‘package:dplyr’:
# 
#     arrange, count, desc, failwith, id, mutate, rename, summarise, summarize

我们应该经常阅读这些警告:)。现在让我们看看会发生什么:

df %>% 
   group_by(date = cut(date, breaks = "1 hour")) %>%
   summarize(co = mean(co), co2 = mean(co2), VOC = mean(VOC), RH = mean(RH), ugm3 = mean(ugm3), temp = mean(temp))
#     co   co2      VOC     RH   ugm3   temp
# 1 0.149 546.6 22.96384 77.106 12.823 61.916

如果您在dplyr后加载plyr,或使用dplyr::summarize,则会产生预期的行为。

df %>% 
   group_by(date = cut(date, breaks = "1 hour")) %>%
   dplyr::summarize(co = mean(co), co2 = mean(co2), VOC = mean(VOC), RH = mean(RH), ugm3 = mean(ugm3), temp = mean(temp))
# # A tibble: 2 x 7
#                  date    co     co2      VOC    RH    ugm3   temp
#                <fctr> <dbl>   <dbl>    <dbl> <dbl>   <dbl>  <dbl>
# 1 2017-09-12 03:00:00 0.149 545.375 22.97435 77.06 12.8425 61.945
# 2 2017-09-12 07:00:00 0.149 551.500 22.92180 77.29 12.7450 61.800