Reading and Writing zoo Series
write.zoo are convenience functions for reading
"zoo" series from/to text files. They are convenience
read.zoo(file, format = "", tz = "", FUN = NULL, regular = FALSE, index.column = 1, aggregate = FALSE, ...) write.zoo(x, file = "", index.name = "Index", row.names = FALSE, col.names = NULL, ...)
- character giving the name of the file which the data
are to be read from/written to. See
write.tablefor more information.
- date format argument passed to
- time zone argument passed to
- a function for computing the index from the first column of the data. See details.
- logical. Should the series be coerced to class
"zooreg"(if the series is regular)?
- integer. The column of the data frame in which the index/time is stored.
- character with name of the index column in the written data file.
- logical. Should row names be written? Default is
FALSEbecause the row names are just character representations of the index.
- logical. Should column names be written? Default is to
write column names only if
xhas column names.
- logical or function. If set to
aggregate.zoois applied to the zoo object created to compute the
meanof all values with t
- further arguments passed to
read.zoo is a convenience function which should make it easier
to read data from a text file and turn it into a
read.zoo reads the data file via
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 uses the first of the following that is
true: 1. If
FUN is specified then
the index as the first argument. 2. If
tz is specified then the index
column is converted to
POSIXct. 3. If
format is specified
then the index column is converted to
Date. 4. A heuristic
attempts to decide among
tz is specified
then it is passed to the conversion function as well.
regular is set to
TRUE and the resulting series has an
underlying regularity, it is coerced to a
write.zoo is a convenience function for writing
to text files. It first coerces its argument to a
a column with the index and then calls
read.zooreturns an object of class
read.zoo works by first reading the data in using
and then processing it. This implies that
if the index field is entirely numeric the default is to pass it to
or the builtin date conversion routine
a number, rather than a character string.
Thus a date field such as
to represent December 12, 2007 would be seen as
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
index column in the
"colClasses" argument, which is passed to
as shown in the examples below.
## turn *numeric* first column into yearmon index ## where number is year + fraction of year represented by month z <- read.zoo("foo.csv", sep = ",", FUN = as.yearmon) ## first column is of form yyyy.mm ## (Here we use format in place of as.character so that final zero ## is not dropped in dates like 2001.10 which as.character would do.) f <- function(x) as.yearmon(format(x, nsmall = 2), "%Y.%m") z <- read.zoo("foo.csv", header = TRUE, FUN = f) ## turn *character* first column into "Date" index ## Assume lines look like: 12/22/2007 1 2 z <- read.zoo("foo.tab", format = "%m/%d/%Y") # ensure that first column is interpreted as character so leading 0 not dropped # Suppose links look like: 09112007 1 2 and there is no header z <- read.zoo("foo.txt", format = "%d%m%Y", colClasses = c(V1 = "character")) ## csv file with first column of form YYYY-mm-dd HH:MM:SS ## Read in times as "chron" class. Requires chron 2.3-22 or later. z <- read.zoo("foo.csv", header = TRUE, sep = ",", FUN = as.chron) ## same file format but read it in times as "POSIXct" class. z <- read.zoo("foo.csv", header = TRUE, sep = ",", tz = "") ## csv file with first column mm-dd-yyyy. Read times as "Date" class. z <- read.zoo("foo.csv", header = TRUE, sep = ",", format = "%m-%d-%Y") ## whitespace separated file with first column of form YYYY-mm-ddTHH:MM:SS ## and no headers. T appears literally. Requires chron 2.3-22 or later. z <- read.zoo("foo.csv", FUN = as.chron)