按多个变量分组并汇总dplyr

编程入门 行业动态 更新时间:2024-10-25 22:31:03
本文介绍了按多个变量分组并汇总dplyr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试每30秒对每个传感器平均CO2浓度数据:

I'm trying to average CO2 concentration data every 30 seconds, for each of my sensors:

head(df) # A tibble: 6 x 7 # Groups: BinnedTime [1] Sensor Date Time calCO2 DeviceTime cuts BinnedTime <fctr> <date> <time> <dbl> <dttm> <fctr> <chr> 1 N1 2019-02-12 13:24 400 2019-02-12 13:24:02 (0,10] 2019-02-12 13:24:02 2 N1 2019-02-12 13:24 400 2019-02-12 13:24:02 (0,10] 2019-02-12 13:24:02 3 N1 2019-02-12 13:24 400 2019-02-12 13:24:03 (0,10] 2019-02-12 13:24:03 4 N2 2019-02-12 13:24 400 2019-02-12 13:24:03 (0,10] 2019-02-12 13:24:02 5 N3 2019-02-12 13:24 400 2019-02-12 13:24:03 (0,10] 2019-02-12 13:24:02 6 N3 2019-02-12 13:24 400 2019-02-12 13:24:05 (0,10] 2019-02-12 13:24:04

我使用:

df %>% group_by(Sensor)%>% group_by(BinnedTime = cut(DeviceTime, breaks="30 sec")) %>% summarize(Concentration = mean(calCO2))

但是它不会先按Sensor分组,它会忽略它们并计算BinnedTime的平均值

But it doesn't group by Sensor first, it ignores them and calculates the average over the BinnedTime instead. Any thoughts would be welcomed.

我已经读过有关 .dots = c( Sensor, BinnedTime)但t他不起作用。

I've read about .dots=c("Sensor","BinnedTime") but this doesn't work.

注意,我还没有创建伪数据,因此您可以确切地看到我的模样,因为时间和时间似乎有些微妙。

Note, I haven't created dummy data so you can see exactly what mine looks like, as there seem to be some subtleties with time and date that I can't quite get my head around.

推荐答案

所以总结@kath的评论并做了一些改进,以解决这个问题。您的后续问题:

So to summarize the comments by @kath with some improvements to address your follow-on question:

df %>% group_by(Sensor, BinnedTime = cut(DeviceTime, breaks="30 sec")) %>% mutate(Concentration = mean(calCO2)) %>% ungroup()

以上内容将保留所有列,但将df的每一行的浓度计算重复。可以让您汇总并保留更多感兴趣的列的替代方法是,将它们简单地添加到汇总操作中,如下所示。

The above will maintain all columns, but duplicate the Concentration calculation for each row of the df. An alternative that would allow you to both roll up and retain more columns of interest is to simply add them to the summarize operation, as illustrated below.

df %>% group_by(Sensor, BinnedTime = cut(DeviceTime, breaks="30 sec")) %>% summarize(Concentration = mean(calCO2), Date = min(Date), Time = min(Time), StartDeviceTime = min(DeviceTime), EndDeviceTime = max(DeviceTime))

更多推荐

按多个变量分组并汇总dplyr

本文发布于:2023-11-25 18:36:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630883.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   变量   dplyr

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!