如何为每组以5分钟间隔汇总数据?

时间:2019-06-08 09:21:15

标签: r group-by aggregate

这是我的数据。

a <-
structure(list(timestamp1.x = c("2019-05-31 18:27:34", "2019-05-31
18:28:34",  "2019-05-31 18:29:34", "2019-05-31 18:29:59", "2019-05-31
18:35:35",  "2019-05-31 18:35:43", "2019-05-31 18:41:43", "2019-05-31
18:42:45",  "2019-05-31 18:49:34", "2019-05-31 18:50:34"), sensor =
c("A",  "A", "A", "A", "B", "B", "C", "C", "A", "A"), direction =
c(-75, 
-78, -58, -54, -72, -47, -57, -51, -75, -78)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

我想为每个传感器以5分钟间隔汇总数据。 这是审判。

aa <- a %>%
  mutate(timestamp1.x = as.POSIXct(timestamp1.x, format = "%Y-%m-%d %H:%M:%S")) %>%
  group_by(sensor, timestamp1.x = cut(timestamp1.x, breaks="5 min")) %>%
  summarize(mean_direction = mean(direction))

这是结果表。

  sensor timestamp1.x        mean_direction
  <chr>  <fct>                        <dbl>
1 A      2019-05-31 18:27:00          -66.2
2 A      2019-05-31 18:47:00          -76.5
3 B      2019-05-31 18:32:00          -59.5
4 C      2019-05-31 18:37:00          -57  
5 C      2019-05-31 18:42:00          -51  

但是,我想对每个组(传感器)进行汇总。

这意味着,对于传感器A,数据应在18:27:00到18:31:59(5分钟间隔)内汇总。对于传感器B,应该汇总从18:35:00到18:39:49(5分钟间隔)的数据。

这是我想要的结果。

  sensor timestamp1.x        mean_direction
  <chr>  <fct>                        <dbl>
1 A      2019-05-31 18:27:00          -66.2
2 A      2019-05-31 18:49:00          -76.5
3 B      2019-05-31 18:35:00          -59.5
4 C      2019-05-31 18:41:00          -54

如何在“ group_by”或其他代码中添加一些功能和选项?

2 个答案:

答案 0 :(得分:1)

以下内容是问题的要求。
诀窍是仅按l=[[0, 'Name', 'title'],[2, 'Name', 'title'],[1, 'Name', 'title']] print([i for i in l if i[0]!=2]) #[[0, 'Name', 'title'], [1, 'Name', 'title']] 分组,然后按sensor cut分组,然后按timestamp1.xsensor分组。

timestamp1.x

数据创建代码。

bb <- b %>%
  mutate(timestamp1.x = as.POSIXct(timestamp1.x, format = "%Y-%m-%d %H:%M:%S")) %>%
  group_by(sensor) %>%
  mutate(timestamp1.x = as.character(cut(timestamp1.x, breaks="5 min"))) %>%
  ungroup() %>%
  group_by(sensor, timestamp1.x) %>%
  summarize(mean_direction = mean(direction)) 

答案 1 :(得分:1)

您需要顺序添加分组。因此,首先group_by(sensor),然后group_by(timestamp1.x = cut(...), add = T)

a%>%
  mutate(timestamp1.x = as.POSIXct(timestamp1.x))%>%
  group_by(sensor)%>%
  group_by(timestamp1.x = as.character(cut(timestamp1.x, breaks="5 min")),add = T)%>%
  summarize(mean_direction = mean(direction))

编辑:在group_by通话中添加了as.character。以前,它将在sensor期间删除summarize字段。现在可以正常工作了。