R语言 以每分钟为基础求出一个数字的平均值

jrcvhitl  于 2023-02-01  发布在  其他
关注(0)|答案(2)|浏览(165)

我有一个数据框,包含以下内容:

Id     final_date_time Speed
2022484408 2016-04-12 07:21:00    97
2022484405 2016-04-12 07:21:05   102
2022484402 2016-04-12 07:21:10   105
2022484407 2016-04-12 07:21:20   103
2022484408 2016-04-12 07:21:25   101
2022484408 2016-04-12 07:22:05    95
2022484410 2016-04-12 07:22:10    91
2022484408 2016-04-12 07:22:15    93
2022484421 2016-04-12 07:22:20    94
2022484408 2016-04-12 07:22:25    93
2022484421 2016-04-12 07:22:35    92
2022484421 2016-04-12 07:22:40    89
2022484447 2016-04-12 07:22:50    83
2022484488 2016-04-12 07:22:55    61
2022484411 2016-04-12 07:23:00    60

“最终日期时间”的类是POSIXct。
我怎样才能找到每分钟的“速度”平均值?
下面是dput信息:

structure(list(Id = c(2022484408, 2022484408, 2022484408, 2022484408, 
2022484408, 2022484408), new_final_date = structure(c(1460438460, 
1460438465, 1460438470, 1460438480, 1460438485, 1460438525), class = c("POSIXct", 
"POSIXt"), tzone = ""), Value = c(97L, 102L, 105L, 103L, 101L, 
95L)), row.names = c(NA, 6L), class = "data.frame")

我尝试了一个解决以前类似问题的解决方案,(更改了标签),但我无法让它工作:

library(xts)
x <- as.xts(dd[,date := as.POSIXct(date)])
period.apply(x, endpoints(x, "minutes", 15), mean)
6jygbczu

6jygbczu1#

如果您切换到data.table,这将变得非常容易。为此,我们首先纠正您的问题,使数据可读。我也使用data.table,并在实际上不可读的数据中添加逗号:具有三个列名的四个实际列:

> D <- fread(text="Id,    final_date_time, Speed
+ 2022484408, 2016-04-12 07:21:00,    97
+ 2022484405, 2016-04-12 07:21:05,   102
+ 2022484402, 2016-04-12 07:21:10,   105
+ 2022484407, 2016-04-12 07:21:20,   103
+ 2022484408, 2016-04-12 07:21:25,   101
+ 2022484408, 2016-04-12 07:22:05,    95
+ 2022484410, 2016-04-12 07:22:10,    91
+ 2022484408, 2016-04-12 07:22:15,    93
+ 2022484421, 2016-04-12 07:22:20,    94
+ 2022484408, 2016-04-12 07:22:25,    93
+ 2022484421, 2016-04-12 07:22:35,    92
+ 2022484421, 2016-04-12 07:22:40,    89
+ 2022484447, 2016-04-12 07:22:50,    83
+ 2022484488, 2016-04-12 07:22:55,    61
+ 2022484411, 2016-04-12 07:23:00,    60")
> D
            Id     final_date_time Speed
         <int>              <POSc> <int>
 1: 2022484408 2016-04-12 07:21:00    97
 2: 2022484405 2016-04-12 07:21:05   102
 3: 2022484402 2016-04-12 07:21:10   105
 4: 2022484407 2016-04-12 07:21:20   103
 5: 2022484408 2016-04-12 07:21:25   101
 6: 2022484408 2016-04-12 07:22:05    95
 7: 2022484410 2016-04-12 07:22:10    91
 8: 2022484408 2016-04-12 07:22:15    93
 9: 2022484421 2016-04-12 07:22:20    94
10: 2022484408 2016-04-12 07:22:25    93
11: 2022484421 2016-04-12 07:22:35    92
12: 2022484421 2016-04-12 07:22:40    89
13: 2022484447 2016-04-12 07:22:50    83
14: 2022484488 2016-04-12 07:22:55    61
15: 2022484411 2016-04-12 07:23:00    60
>

现在我们已经解决了这个问题,我们将Datetime * 舍入 * 为分钟(并确保在单列中返回POSIX),然后使用data.table运行一个简单的聚合(这里是:mean())按四舍五入的分钟数。

> D[, min := as.POSIXct(round(final_date_time, "mins"))][, .(avgspeed=mean(Speed)), by=min]
                   min avgspeed
                <POSc>    <num>
1: 2016-04-12 07:21:00    101.6
2: 2016-04-12 07:22:00     93.2
3: 2016-04-12 07:23:00     77.0
>

这段代码是一行代码,但在这里看起来更好,它可以有一个换行符,也可以自愿分成两行:

> D[, min := as.POSIXct(round(final_date_time, "mins"))]
> D[, .(avgspeed=mean(Speed)), by=min]

有一些以R为基的习惯用法可以通过aveaggregate来做同样的事情,当然也有dplyr调用,我碰巧更喜欢data.table

jyztefdp

jyztefdp2#

library(data.table)

setDT(x)

x[, .(avg = mean(Speed)), .(Id, interval = cut(final_date_time, breaks = "1 min"))]

#             Id            interval     avg
# 1:  2022484408 2016-04-12 07:21:00  99.000
# 2:  2022484405 2016-04-12 07:21:00 102.000
# 3:  2022484402 2016-04-12 07:21:00 105.000
# 4:  2022484407 2016-04-12 07:21:00 103.000
# 5:  2022484408 2016-04-12 07:22:00  93.667
# 6:  2022484410 2016-04-12 07:22:00  91.000
# 7:  2022484421 2016-04-12 07:22:00  91.667
# 8:  2022484447 2016-04-12 07:22:00  83.000
# 9:  2022484488 2016-04-12 07:22:00  61.000
# 10: 2022484411 2016-04-12 07:23:00  60.000

*我假设您也要按ID分组,如果不是,请删除Id

x[, .(avg = mean(Speed)), .(interval = cut(final_date_time, breaks = "1 min"))]

相关问题