【发布时间】:2019-01-25 13:43:49
【问题描述】:
我正在尝试计算基于 10 分钟数据的数据框中的每 3 个观察值,我试图将其平均到半小时。我的数据如下所示:
Date Value
2017-09-20 09:19:59 96.510
2017-09-20 09:30:00 113.290
2017-09-20 09:40:00 128.370
2017-09-20 09:50:00 128.620
2017-09-20 10:00:00 94.080
2017-09-20 10:10:00 208.150
2017-09-20 10:20:00 178.820
2017-09-20 10:30:00 208.440
2017-09-20 10:40:00 285.490
2017-09-20 10:49:59 305.020
我首先尝试使用 zoo 包 library (zoo) 中的函数 rollapply 按以下方式计算均值:
means <- rollapply(df, by=3, 3, FUN=mean)
但是,我收到了 50 条警告:
在 mean.default(data[posns], ...) 中:参数不是数字或 逻辑:返回 NA
我检查了我的课程,值(数字)和日期是一个因素。然后我尝试通过以下方式将日期(因子)转换为日期类:
`df$Date <- as.Date(df, format = "%Y-%m-%d %H:%m:%s")` and
df$Date <- strptime(time,"%Y-%m-%d %H:%M:%S",tz="GMT") and still didn't work.
我也试过用聚合来计算均值,但还是不行。
library(chron)
aggregate(chron(times=Date) ~ Value, data=df, FUN=mean)
我得到了:
convert.times(times., fmt) 中的错误:格式 h:m:s 可能不正确 另外:警告消息:在 convert.times(times., fmt) : NAs 强制引入
此时我很绝望,很抱歉在这里问。也许我的数据有问题,因为它首先是一个 xlxs 文件,我将奇怪的 excel 时间转换为 R 中的日期,但仍然......我想知道,因为这是因为某些日期的末尾有 :59 秒.如果有帮助,我也可以在线发布我的全部数据。非常感谢!
【问题讨论】:
标签: r dataframe aggregate coercion rollapply