## this manual page provides a few typical examples, many more cases
## are covered in vignette("zoo-read", package = "zoo")
## read text lines with a single date column
Lines <- "2013-12-24 2
2013-12-25 3
2013-12-26 8"
read.zoo(text = Lines, FUN = as.Date) # explicit coercion
read.zoo(text = Lines, format = "%Y-%m-%d") # same
read.zoo(text = Lines) # same, via heuristic
## read text lines with date/time in separate columns
Lines <- "2013-11-24 12:41:21 2
2013-12-25 12:41:22.25 3
2013-12-26 12:41:22.75 8"
read.zoo(text = Lines, index = 1:2,
FUN = paste, FUN2 = as.POSIXct) # explicit coercion
read.zoo(text = Lines, index = 1:2, tz = "") # same
read.zoo(text = Lines, index = 1:2) # same, via heuristic
## read directly from a data.frame (artificial and built-in BOD)
dat <- data.frame(date = paste("2000-01-", 10:15, sep = ""),
a = sin(1:6), b = cos(1:6))
read.zoo(dat)
data("BOD", package = "datasets")
read.zoo(BOD)
## Not run:
# ## descriptions of typical 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)
#
# # read in all csv files in the current directory and merge them
# read.zoo(Sys.glob("*.csv"), header = TRUE, sep = ",")
#
# # 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(text = 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)
# ## End(Not run)
Run the code above in your browser using DataLab