Effectively, the default index class is "Date"
unless tz
is used in which case the default is "POSIXct"
. Thus the first example in the question gives "Date"
class since that is the default and the second "POSIXct"
since tz
was specified.
If you want to specify the class without making use of these defaults then to be explicit use the FUN
argument:
read.zoo(...whatever..., FUN = as.Date)
read.zoo(...whatever..., FUN = as.POSIXct) # might need FUN=paste,FUN2=as.POSIXct
read.zoo(...whatever..., FUN = as.yearmon)
# etc.
The FUN
argument can also take a custom function as shown in the examples in the package.
Note that it always assumes standard formats (e.g. "%Y-%m-%d"
in the case of "Date"
class) if no format is specified and never tries to automatically determine the format.
The way it works is explained in detail in ?read.zoo
and there are many examples in ?read.zoo
(there are 78 lines of code in the examples section) as well as in an entire vignette (one of six vignettes) dedicated just to read.zoo
" : Reading Data in zoo.
Added Have expanded the above. Also, in the development version of zoo available here the heuristic has been improved and with that improvement the first example in the question does recognize the date/times and chooses POSIXct. Also some clarification of the simple heuristic has been added to the read.zoo
help file so that the many examples provided do not have to be relied upon as much.
Here are some examples. Note that the heuristic referred to is a heuristic to determine the class of the time index only. It can only identify "numeric"
, "Date"
and "POSIXct"
classes. The heuristic cannot identify other classes (although you can specify them yourself using FUN=
). Also the heuristic does not identify formats. If the format is not provided using format=
or implicitly through FUN=
then standard format is assumed, e.g. "%Y-%m-%d"
in the case of "Date"
.
Lines <- "2013-11-25 12:41:21 2
2013-12-25 12:41:22.25 3
2013-12-26 12:41:22.75 8"
# explicit. Uses POSIXct.
z <- read.zoo(text = Lines, index = 1:2, FUN = paste, FUN2 = as.POSIXct)
# tz implies POSIXct
z <- read.zoo(text = Lines, index = 1:2, tz = "")
# heuristic: Date now; devel ver uses POSIXct
z <- read.zoo(text = Lines, index = 1:2)
Lines <- "2013-11-251 2
2013-12-25 3
2013-12-26 8"
z <- read.zoo(text = Lines, FUN = as.Date) # explicit. Uses Date.
z <- read.zoo(text = Lines, format = "%Y-%m-%d") # format & no tz implies Date
z <- read.zoo(text = Lines) # heuristic: Date
Note:
(1) In general, its safer to be explicit by using FUN
or by using tz
and/or format
as opposed to relying on the heuristic. If you are explicit by using FUN
or semi-explicit by using tz
and/or format
then there is no change between the current and the development versions of read.zoo
.
(2) Its safer to rely on the documentation rather than the internals as the internals can change without warning and in fact have changed in the development version. If you really want to look at the code despite this then the key statement that selects the class of the index if FUN
is not explicitly defined is the if (is.null(FUN)) ...
statement in the read.zoo
source.
(3) I recommend using read.zoo
as being easier, direct and compact rather than workarounds such as read.table
followed by zoo
. I have been using read.zoo
for years as have many others and it seems pretty solid to me but if anyone finds specific problems with read.zoo
or with the documentation (always possible since there is quite a bit of it) they can always be reported. Even though the package has been around for years improvements are still being made.