zoo (version 1.4-1)

read.zoo: Reading and Writing zoo Series

Description

read.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.

Usage

read.zoo(file, format = "", tz = "", FUN = NULL, regular = FALSE, index.column = 1, ...)
write.zoo(x, file = "", index.name = "Index", row.names = FALSE, col.names = NULL, ...)

Arguments

file
character giving the name of the file which the data are to be read from/written to. See read.table and write.table for more information.
format
date format argument passed to as.Date.character.
tz
time zone argument passed to as.POSIXct.
FUN
a function for computing the index from the first column of the data. See details.
regular
logical. Should the series be coerced to class "zooreg" (if the series is regular)?
index.column
integer. The column of the data frame in which the index/time is stored.
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 FALSE because 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 x has column names.
...
further arguments passed to read.table or write.table, respectively.

Value

  • read.zoo returns an object of class "zoo" (or "zooreg").

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. By default, read.zoo uses as.Date(as.character(x), format = format) if format is specified and as.POSIXct(as.character(x), tz = tz) if tz is specified; otherwise it tries to guess between "numeric", "Date" and "POSIXct".

If regular is set to TRUE and the resulting series has an underlying regularity, it is coerced to a "zooreg" series.

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

zoo

Examples

Run this code
## 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), "z <- read.zoo("foo.csv", header = TRUE, FUN = f)

## turn *character* first column into Date index
z <- read.zoo("foo.tab", format = "%m/%d/%Y")

Run the code above in your browser using DataLab