简体   繁体   中英

How do I convert a list of xts objects in R to weekly averages using period.apply?

I am trying to create weekly averages based off of the the xts objects that I have split into a list using, but I keep getting the error:

Error in isOrdered(INDEX) : 
  (list) object cannot be coerced to type 'double'

I have tried using the period.apply function.

> 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'

What I would like is a weekly average for each xts object on the list. Any help would be greatly appreciated.

It is easier to first split , then transform to xts . Also instead of using period.apply , you can use apply.weekly . Below code shows how all in separate steps. I'm not using any tidyverse functions like map , but will just use lapply to achieve the desired results.

# split on laneid
lanes <- split(rate_data, rate_data$laneid)

# turn list in an list of xts objects without laneid in the matrix. 
# Otherwise the matrix will be a character matrix
lanes_xts <- lapply(lanes, function(x) xts(x[, 3], order.by = x[, 1]))

# use apply.weekly to apply a mean over the weeks.
lanes_weekly <- lapply(lanes_xts, apply.weekly, mean)

As for the error you are getting. The line map(lanes_xts, endpoints, on = 'weeks') in your code returns a list of endpoints. This can not be passed on in period.apply . That is why it returns an error as the list cannot be forced into a vector of endpoints.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM