Reading and Writing zoo Series
read.zoo.Rdread.zoo and write.zoo are convenience functions for reading
and writing "zoo" series from/to text files. They are convenience
interfaces to read.table and write.table, respectively.
To employ read.csv, read.csv2, read.delim,
read.delim2 instead of read.table additional functions
read.csv.zoo etc. are provided.
Usage
read.zoo(file, format = "", tz = "", FUN = NULL,
regular = FALSE, index.column = 1, drop = TRUE, FUN2 = NULL,
split = NULL, aggregate = FALSE, ..., text, read = read.table)
write.zoo(x, file = "", index.name = "Index", row.names = FALSE, col.names = NULL,
FUN = format, ...)
read.csv.zoo(..., read = read.csv)
read.csv2.zoo(..., read = read.csv2)
read.delim.zoo(..., read = read.delim)
read.delim2.zoo(..., read = read.delim2)Arguments
- file
character string or strings giving the name of the file(s) which the data are to be read from/written to. See
read.tableandwrite.tablefor more information. Alternatively, inread.zoo,filecan be aconnectionor adata.frame(e.g., resulting from a previousread.tablecall) that is subsequently processed to a"zoo"series.- format
date format argument passed to
FUN.- tz
time zone argument passed to
as.POSIXct.- FUN
a function for processing the time index. In
read.zoothis is the function that computes the index from theindex.column, see the details. Inwrite.zooit is a function that formats the index prior to writing it tofile.- regular
logical. Should the series be coerced to class
"zooreg"(if the series is regular)?- index.column
numeric vector or list. The column names or numbers of the data frame in which the index/time is stored. If the
read.tableargumentcolClassesis used and"NULL"is among its components thenindex.columnrefers to the column numbers after the columns corresponding to"NULL"incolClasseshave been removed. If specified as a list then one argument will be passed to argumentFUNper component so that, for example,index.column = list(1, 2)will causeFUN(x[,1], x[,2], ...)to be called whereasindex.column = list(1:2)will causeFUN(x[,1:2], ...)to be called wherexis a data frame of characters data. Here...refers toformatand/ortz, if they specified as arguments.index.column = 0can be used to specify that the row names be used as the index. In the case that no row names were input sequential numbering is used. Ifindex.columnis specified as an ordinary vector then if it has the same length as the number of arguments ofFUN(orFUN2in the event thatFUN2is specified andFUNis not) thenindex.columnis converted to a list. Also it is always converted to a list if it has length 1.- drop
logical. If the data frame contains just a single data column, should the second dimension be dropped?
- x
a
"zoo"object.- index.name
character with name of the index column in the written data file.
- row.names
logical. Should row names be written? Default is
FALSEbecause the row names are just character representations of the index.- col.names
logical. Should column names be written? Default is to write column names only if
xhas column names.- FUN2
function. It is applied to the time index after
FUNand beforeaggregate. IfFUNis not specified butFUN2is specified then onlyFUN2is applied.- split
NULL or column number or name or vector of numbers or names. If not NULL then the data is assumed to be in long format and is split according to the indicated columns. See the R
reshapecommand for description of long data. Ifsplit = Infthen the first of each run among the times are made into a separate series, the second of each run and so on. Ifsplit= -Infthen the last of each run is made into a separate series, the second last and so on.- aggregate
logical or function. If set to
TRUE, thenaggregate.zoois applied to the zoo object created to compute themeanof all values with the same time index. Alternatively,aggregatecan be set to any other function that should be used for aggregation. IfFALSE(the default), no aggregation is performed and a warning is given if there are any duplicated time indexes. Note that mostzoofunctions do not accept objects with duplicate time indexes. Seeaggregate.zoo.- ...
further arguments passed to other functions. In the
read.*.zoothe arguments are passed to the function specified inread(unlessfileis adata.framealready). Inwrite.zoothe arguments are passed towrite.table.- text
character. If
fileis not supplied and this is, then data are read from the value oftextvia a text connection. See below for an example.- read
function. The function for reading
file(unless it is adata.framealready).
Details
read.zoo is a convenience function which should make it easier
to read data from a text file and turn it into a "zoo" series
immediately. read.zoo reads the data file via read.table(file, ...).
The column index.column (by default the first) of the resulting data is
interpreted to be the index/time, the remaining columns the corresponding data.
(If the file only has only column then that is assumed to be the data column and
1, 2, ... are used for the index.) To assign the appropriate class
to the index, FUN can be specified and is applied to the first column.
To process the index, read.zoo calls FUN with the index as the
first argument. If FUN is not specified, the following default is employed:
(a) If file is a data frame with a single
index column that appears to be a time index already, then FUN = identity is used.
The conditions for a readily produced time index are: It is not character or
factor (and the arguments tz and format must not be specified).
(b) If the conditions from (a) do not hold then the following strategy is used.
If there are multiple index columns they are pasted together with a space between each.
Using the (pasted) index column: (1) If tz is specified then the
index column is converted to POSIXct. (2) If format is specified
then the index column is converted to Date. (3) Otherwise, a heuristic
attempts to decide between "numeric", "POSIXct", and "Date" by
trying them in that order (which may not always succeed though). By default,
only the standard date/time format is used. Hence, supplying format and/or tz
is necessary if some date/time format is used that is not the default. And even
if the default format is appropriate for the index, explicitly supplying
FUN or at least format and/or tz typically leads to more
reliable results than the heuristic.
If regular is set to TRUE and the resulting series has an
underlying regularity, it is coerced to a "zooreg" series.
To employ other functions than read.table to read the initial data,
further convenience interfaces read.csv.zoo etc. are provided.
write.zoo is a convenience function for writing "zoo" series
to text files. It first coerces its argument to a "data.frame", adds
a column with the index and then calls write.table.
See also vignette("zoo-read", package = "zoo") for detailed examples.
Note
read.zoo works by first reading the data in using read.table
and then processing it. This implies that
if the index field is entirely numeric the default is to pass it to FUN
or the built-in date conversion routine
a number, rather than a character string.
Thus, a date field such as 09122007 intended
to represent September 12, 2007 would be seen as 9122007
and interpreted as the 91st day
thereby generating an error.
This comment also applies to trailing decimals so that if
2000.10 were intended to represent the 10th month of 2000 in fact
it would receive
2000.1 and regard it as the first month of 2000
unless similar precautions were taken.
In the above cases the index field should be specified to be
"character" so that leading or trailing zeros
are not dropped. This can be done by specifying a "character"
index column in the
"colClasses" argument, which is passed to read.table,
as shown in the examples below.