Skip to content

How can I read time series data to create zoo objects if I have two title lines?

2 messages · John, Gabor Grothendieck

#
On Fri, Aug 3, 2012 at 4:33 AM, jpm miao <miaojpm at gmail.com> wrote:
Record the second line (i.e. the transforms) as a comment, read in the
second line using a second pass and then apply the transforms.  We
used text = Lines to keep this self contained but in reality each
occurrence of text = Lines would be replaced with something like file
= "myfile.dat". .)

library(zoo)

Lines <- "Time a b
# identity log10
198101 1 10
198102 2 100"

# read data
asYearmon <- function(x) as.yearmon(as.character(x), "%Y%m")
z <- read.zoo(text = Lines, header = TRUE, FUN = asYearmon)

# read transforms
transforms <- read.table(text = Lines, nrow = 2, as.is = TRUE,
	comment = "")[2, -1]

# apply transforms
z[] <- sapply(seq_along(transforms), function(i)
	do.call(transforms[[i]], list(z[, i])))