简体   繁体   中英

Why does “xts” in R cannot recognize my date series?

births and dates are from same file and share same length. format of dates seems to be good. But xts cannot recogize it as date,why?! Thanks for your help!

dates <- read.xlsx("TimeSeriesCourseworkData.xls", 1,colIndex=1,as.data.frame=TRUE)
dates

        Month
1  2009-01-01
2  2009-02-01
3  2009-03-01
4  2009-04-01
............

26 2011-02-01
27 2011-03-01
28 2011-04-01
29 2011-05-01
30 2011-06-01

births <- xts(births, order.by = dates) 
# Error in xts(births, order.by = dates) : 
# order.by requires an appropriate time-based object

births <- xts(births, order.by = as.Date(dates))
# Error in as.Date.default(dates) : 
# do not know how to convert 'dates' to class “Date”

dates is a 1-column data.frame. as.Date and xts' order.by argument require vectors.

births <- xts(births, dates$Month)

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