简体   繁体   中英

R Using timeSeries::timeSeries()

Can anyone point out why code chunk (1) works but not chunk (2)?

Code chunk (1):

library(timeSeries)
data <- matrix(round(rnorm(24), 4), nrow = 12)
dates <- seq(as.Date("2009-01-01"), by = "month", length.out = 12)
ts <- timeSeries(data, dates)

Code chunk (2):

library(timeSeries)
data <- matrix(round(rnorm(50), 4), nrow = 10)

dput(dates)
structure(list(date = structure(c(14977, 14978, 14979, 14980, 
14981, 14984, 14985, 14986, 14987, 14988), class = "Date")), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

ts <- timeSeries(data, dates)

R slaps my hand with this:

Warning message: In whichFormat(charvec[1]): character string is not in a standard unambiguous format

From ?timeSeries , charvec is a vector. In the second case, you are passing it a tibble/dataframe which is why you get an error. Try:

library(timeSeries)
ts <- timeSeries(data, dates$date)
ts

GMT
#              TS.1    TS.2    TS.3    TS.4    TS.5
#2011-01-03  0.3524 -0.7522 -0.8783 -1.2821  0.4415
#2011-01-04 -0.4300 -0.3055 -0.1887 -0.2559 -1.5267
#2011-01-05 -0.6599  0.3771 -0.4605 -1.9424 -1.5447
#2011-01-06 -0.6139  0.4265 -0.0856  1.0123 -0.5640
#2011-01-07  0.4265 -0.9353  0.3372  1.1031  0.3113
#2011-01-10  2.1332 -0.0201 -1.1552 -0.8441  0.0602
#2011-01-11  0.4315  1.3390  0.0306 -0.9695  1.2771
#2011-01-12 -0.4005 -0.5906  1.4297 -0.4055  0.1387
#2011-01-13 -1.9644  0.3039 -2.0004 -0.7983 -1.5974
#2011-01-14  0.8797  0.6690 -1.8821  0.0437 -0.2202

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