我正在尝试使用timeAverage()
函数计算数据集的每小时平均值(2014年1月)。该命令如下所示:
AQHr <- timeAverage(my_data, avg.time = "hour", data.thresh = 0,
statistic = "mean", percentile = NA,
start.date = "2014-01-01", end.date = "2014-01-31",
interval = "hour", vector.ws = FALSE)
我收到此错误:
Error in bind_rows_(x, .id) :
Can not automatically convert from POSIXct, POSIXt to Date in column "date".
看起来数据框中的date
列出现了严重问题。列date
采用以下格式:
> str(my_data$date)
Date[1:192840], format: "1993-12-31" "1994-01-01" "1994-01-01" "1994-01-01"
"1994-01-01" "1994-01-01" "1994-01-01" "1994-01-01" "1994-01-01" ...
有人可以建议如何修复此错误吗?
更新1:
以下是示例my_data
:
Sr day month year hour date my_value
1 1 1 1994 1 31/12/1993 70.49488
2 1 1 1994 2 01/01/1994 41.3616
3 1 1 1994 3 01/01/1994 30.99245
4 1 1 1994 4 01/01/1994 30.32162
5 1 1 1994 5 01/01/1994 29.91912
6 1 1 1994 6 01/01/1994 26.94829
7 1 1 1994 7 01/01/1994 28.90329
8 1 1 1994 8 01/01/1994 34.25078
9 1 1 1994 9 01/01/1994 30.32162
10 1 1 1994 10 01/01/1994 26.81412
11 1 1 1994 11 01/01/1994 26.69912
12 1 1 1994 12 01/01/1994 28.00245
13 1 1 1994 13 01/01/1994 28.88412
14 1 1 1994 14 01/01/1994 31.01162
15 1 1 1994 15 01/01/1994 26.71829
16 1 1 1994 16 01/01/1994 29.84245
17 1 1 1994 17 01/01/1994 43.33576
18 1 1 1994 18 01/01/1994 39.21494
19 1 1 1994 19 01/01/1994 33.33078
20 1 1 1994 20 01/01/1994 34.11661
21 1 1 1994 21 01/01/1994 36.07161
22 1 1 1994 22 01/01/1994 36.76161
23 1 1 1994 23 01/01/1994 38.4291
24 1 1 1994 24 01/01/1994 34.80661
使用:
> str(my_data)
'data.frame': 192840 obs. of 6 variables:
$ day : int 1 1 1 1 1 1 1 1 1 1 ...
$ month : int 1 1 1 1 1 1 1 1 1 1 ...
$ year : int 1994 1994 1994 1994 1994 1994 1994 1994 1994 1994 ...
$ hour : int 1 2 3 4 5 6 7 8 9 10 ...
$ date : Date, format: "1993-12-31" "1994-01-01" "1994-01-01" "1994-01-01" ...
$ my_value : num 70.5 41.4 31 30.3 29.9 ...
答案 0 :(得分:0)
包裹&#34;露天&#34;使用POSIXct格式作为日期。
使用以下内容更改格式:
library(dplyr)
AQHr <- AQHr %>%
mutate(date = paste(year, month, day, hour, sep = "-")) %>%
mutate(date = as.POSIXct(strptime(date, format = "%Y-%m-%d-%H", tz = "")))