【发布时间】:2019-06-26 03:02:51
【问题描述】:
我正在尝试根据已拆分为列表的 xts 对象创建每周平均值,但我不断收到错误消息:
Error in isOrdered(INDEX) :
(list) object cannot be coerced to type 'double'
我尝试过使用 period.apply 函数。
> str(rate_data)
'data.frame': 887079 obs. of 3 variables:
$ LoadDate : Date, format: "2018-03-05" "2018-02-21" "2018-02-07" ...
$ laneid : Factor w/ 6905 levels " _FL_Van"," _PA_Van",..: 4629
579 6538 5944 1213 1213 6029 5564 4287 5745 ...
$ TruckPayPerMile: num 3.62 1.5 1.33 2.39 1.01 ...
> head(rate_data)
LoadDate laneid TruckPayPerMile
1 2018-03-05 OH_NY_Van 3.6231
2 2018-02-21 CA_AR_Reefer 1.5046
3 2018-02-07 WA_TX_Van 1.3333
4 2018-01-31 TX_MA_Van 2.3852
5 2018-01-29 FL_SC_Van 1.0149
6 2018-01-30 FL_SC_Van 1.0683
rate_data_xts <- xts(rate_data, rate_data[ ,-2], order.by = rate_data[ ,2])
lanes_xts <- split(rate_data_xts, rate_data_xts$laneid)
tx_ca_reefer <- lanes_xts[["TX_CA_Reefer"]]
head(tx_ca_reefer)
> head(tx_ca_reefer)
LoadDate laneid TruckPayPerMile
2018-01-28 2018-01-28 TX_CA_Reefer 1.6850
2018-01-28 2018-01-28 TX_CA_Reefer 2.5128
2018-01-29 2018-01-29 TX_CA_Reefer 2.4077
2018-01-29 2018-01-29 TX_CA_Reefer 1.3610
2018-01-29 2018-01-29 TX_CA_Reefer 1.8241
2018-01-29 2018-01-29 TX_CA_Reefer 1.8703
Warning message:
In zoo(rval, index(x)[i]) :
some methods for “zoo” objects do not work if the index entries in
‘order.by’ are not unique
end_points <- map(lanes_xts, endpoints, on = 'weeks')
lanes_weekly_xts <- period.apply(lanes_xts, INDEX = end_points, FUN = mean)
Error in isOrdered(INDEX) :
(list) object cannot be coerced to type 'double'
我想要的是列表中每个 xts 对象的每周平均值。任何帮助将不胜感激。
【问题讨论】:
-
请创建一个可重现的示例。 rate_data 中包含什么?使用
dput将其添加到您的问题中。请参阅here 了解更多信息 -
我已添加有关费率数据的信息。它是一个包含三个变量的数据框。包括一个日期列、一个因子列和一个数值列。
标签: r type-conversion time-series purrr