Why can't the xts function find the as.yearmon function without adding a zoo?

If you have a fresh R session (no packages other than the base ones), and try creating the next xts object ordered by the yearmon class ...

df <- data.frame(date = zoo::as.yearmon(seq.Date(as.Date("2015-01-01"),
                                                 as.Date("2015-12-31"),
                                                 by = "month")),
                 num = rnorm(12, 0, 1))

dates <- df[,1]

xts::xts(as.matrix(df[, -1]), order.by = dates)

The following error appears.

enter image description here

I thought I understood the scope of the R namespace, but in this case I was completely lost. Why does this xts try to call the as.yearmon function when the date object is already a yearmon object? I know that xts depends on the zoo, but is there a reason?

If the zoo is attached, then, of course, the error disappears.

, , , , xts. xts, , R , ( ) . - zoo, .

, - , , , SO, . !

+4
2

xts CRAN. xxt GitHub. , zoo zoo, , NAMESPACE.

R> df <- data.frame(date = zoo::as.yearmon(seq.Date(as.Date("2015-01-01"),
+                                                  as.Date("2015-12-31"),
+                                                  by = "month")),
+                  num = rnorm(12, 0, 1))
R> 
R> dates <- df[,1]
R> 
R> xts::xts(as.matrix(df[, -1]), order.by = dates)
               [,1]
Jan 2015 -1.2141571
Feb 2015 -0.7645339
Mar 2015 -0.7555164
Apr 2015 -0.6596672
May 2015  0.2099139
Jun 2015 -0.3374191
Jul 2015  1.1704935
Aug 2015 -2.2101577
Sep 2015  0.7623118
Oct 2015  0.3643535
Nov 2015 -1.2789485
Dec 2015  1.0316663
R> search()
[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"     
R> packageVersion('xts')
[1] ‘0.9.8
+3

xts 0.10 CRAN

+1

Source: https://habr.com/ru/post/1622547/


All Articles