If you have a fresh R session (no packages attached except those from base) and try to create the following xts object ordered by a 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)
you get the following error.
I thought I understood the R namespace framework, but in this case I'm completely lost. Why is that xts tries to call the as.yearmon function when the dates object is already a yearmon object? I am aware that xts depends on zoo but is that the reason?
If zoo is attached then of course the error goes away.
The reason why I am interested in this issue is that I am creating a package that uses the xts package. One of my functions return a xts object, but I would like my package to only depend on R and import all the other packages - which is described as best practice by Hadley Wickham (as I understand it). However, because of this issue I need to depend the zoo package to make this work.
I'm sure that I'm overlooking something, so I hope a friendly soul here at SO can help explain this issue and present a solution. Thanks!