read.zoo

0th

Percentile

Reading and Writing zoo Series

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.

Keywords
ts
Usage
read.zoo(file, format = "", tz = "", FUN = NULL,
  regular = FALSE, index.column = 1, drop = TRUE, FUN2 = NULL,
  split = NULL, aggregate = FALSE, ...)
write.zoo(x, file = "", index.name = "Index", row.names = FALSE, col.names = NULL, ...)
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.table and write.table
format
date format argument passed to FUN.
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
numeric vector or list. The column(s) of the data frame in which the index/time is stored. If the read.table colClasses argument is used and "NULL" is among its componennts then index.column refers t
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 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.
FUN2
function. It is applied to the time index after FUN and before aggregate. If FUN is not specified but FUN2 is specified then only FUN2 is 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 Rreshape command
aggregate
logical or function. If set to TRUE, then aggregate.zoo is applied to the zoo object created to compute the mean of all values with t
...
further arguments passed to read.table or write.table, respectively.
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 then if there are multiple index columns they are pasted together with a space between each. Using the index column or 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 among "numeric", "Date" and "POSIXct". If format and/or tz is specified then they are passed to the conversion function as well.

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.

Value

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

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

See Also

zoo

Aliases
  • read.zoo
  • write.zoo
Examples
## 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")

# Suppose lines look like: 09112007 1 2 and there is no header
z <- read.zoo("foo.txt", format = "%d%m%Y")

## 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 but with custom format.  Note as.chron uses POSIXt-style## Read in times as "chron" class. Requires chron 2.3-24 or later.
z <- read.zoo("foo.csv", header = TRUE, sep = ",", FUN = as.chron, 
	format = "
## 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)

# We use "NULL" in colClasses for those columns we don't need but in 
# col.names we still have to include dummy names for them.  Of what 
# is left the index is the first three columns (1:3) which we convert 
# to chron class times in FUN and then truncate to 5 seconds in FUN2.  
# Finally we use aggregate = mean to average over the 5 second intervals.
library("chron")

Lines <- "CVX 20070201 9 30 51 73.25 81400 0
CVX 20070201 9 30 51 73.25 100 0
CVX 20070201 9 30 51 73.25 100 0
CVX 20070201 9 30 51 73.25 300 0
CVX 20070201 9 30 51 73.25 81400 0
CVX 20070201 9 40 51 73.25 100 0
CVX 20070201 9 40 52 73.25 100 0
CVX 20070201 9 40 53 73.25 300 0"

z <- read.zoo(textConnection(Lines), 
	colClasses = c("NULL", "NULL", "numeric", "numeric", "numeric", "numeric",
		"numeric", "NULL"), 
	col.names = c("Symbol", "Date", "Hour", "Minute", "Second", "Price", 
		"Volume", "junk"),
	index = 1:3,  # do not count columns that are "NULL" in colClasses
	FUN = function(h, m, s) times(paste(h, m, s, sep = ":")),
	FUN2 = function(tt) trunc(tt, "00:00:05"),
	aggregate = mean)

## omit the read.table() phase and directly supply a data.frame
dat <- data.frame(date = paste("2000-01-", 10:15, sep = ""), a = rnorm(6), b = 1:6)
z <- read.zoo(dat)

## using built-in data frame BOD
read.zoo(BOD)

read.zoo(BOD, FUN = as.Date)

read.zoo(BOD[c(1:6, 1), ], aggregate = mean)

\dontrun{
# read in all csv files in the current directory and merge them
read.zoo(Sys.glob("*.csv"), header = TRUE, sep = ",")
}
Documentation reproduced from package zoo, version 1.7-2, License: GPL-2

Community examples

Looks like there are no examples yet.