Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why read.zoo gives index as dates when times are available

Tags:

r

zoo

posixct

I'm trying to understand my difficulties in the past with inputting zoo objects. The following two uses of read.zoo give different results despite the default argument for tz supposedly being "" and that is the only difference between the two read.zoo calls:

Lines <- "2013-11-25 12:41:21         2 
2013-11-25 12:41:22.25      2 
2013-11-25 12:41:22.75      75 
2013-11-25 12:41:24.22      3 
2013-11-25 12:41:25.22      1 
2013-11-25 12:41:26.22      1"

library(zoo)
z <- read.zoo(text = Lines, index = 1:2)

> dput(z)
structure(c(2L, 2L, 75L, 3L, 1L, 1L), index = structure(c(16034, 
16034, 16034, 16034, 16034, 16034), class = "Date"), class = "zoo")

z <- read.zoo(text = Lines, index = 1:2, tz="")
> dput(z)
structure(c(2L, 2L, 75L, 3L, 1L, 1L), index = structure(c(1385412081, 
1385412082.25, 1385412082.75, 1385412084.22, 1385412085.22, 1385412086.22
), class = c("POSIXct", "POSIXt"), tzone = ""), class = "zoo")
> 
like image 869
IRTFM Avatar asked Dec 07 '22 00:12

IRTFM


2 Answers

The answer (of course) is in the sources for read.zoo(), wherein there is:

....
ix <- if (missing(format) || is.null(format)) {
    if (missing(tz) || is.null(tz)) 
        processFUN(ix)
    else processFUN(ix, tz = tz)
}
else {
    if (missing(tz) || is.null(tz)) 
        processFUN(ix, format = format)
    else processFUN(ix, format = format, tz = tz)
}
....

Even though the default for tz is "", in your first case tz is considered missing (by missing()) and hence processFUN(ix) is used. When you set tz = "", it is no longer missing and hence you get processFUN(ix, tz = tz).

Without looking at the details of read.zoo() this could possibly be handled better by having tz = NULL or tz (no default) in the arguments and then in the code, if tz needs to be set to "" for some reason, do:

if (missing(tz) || is.null(tz)) {
    tz <- ""
}

or perhaps this is not even needed if all the is required is to avoid the confusion about the two different calls?

like image 199
Gavin Simpson Avatar answered Jan 07 '23 05:01

Gavin Simpson


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.

like image 28
G. Grothendieck Avatar answered Jan 07 '23 05:01

G. Grothendieck