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

Gabor Grothendieck ggrothendieck at gmail.com
Fri Aug 3 11:30:13 CEST 2012


On Fri, Aug 3, 2012 at 4:33 AM, jpm miao <miaojpm at gmail.com> wrote:
> Hello,
>
>    This is a standard example in which I read the time series data from a
> csv file and create a zoo object:
>
> x0 <- read.csv(file="CPI.csv", header=TRUE)
> time_0<-as.yearmon("1981-01")+(0:371)/12
> x0zoo<-zoo(x0, time_0)
>
>     The data look like this:
>
>     TIME CPI CPI_food CPI_Clothes CPI_House CPI_Rent  198101 62.1 55.34
> 103.45 65.24 61.43  198102 63.16 56.95 103.21 65.77 61.49  198103 63.44
> 56.98 103.25 66.44 61.98  198104 63.69 57.41 103.55 66.58 62.15  198105
> 63.45 56.77 103.44 66.72 62.32  198106 64.05 58.06 103.35 66.81 62.4  198107
> 64.29 58.53 102.85 66.88 62.5
>     However, time series data usually need to be transformed (e.g., ln
> (natural log) , difference or log difference, etc), I would like to attach
> a line under the title, and the data look like this:
>  TIME CPI CPI_food CPI_Clothes CPI_House CPI_Rent  lv ln dln dln dln ln
> 198101 62.1 55.34 103.45 65.24 61.43  198102 63.16 56.95 103.21 65.77 61.49
> 198103 63.44 56.98 103.25 66.44 61.98  198104 63.69 57.41 103.55 66.58 62.15
> 198105 63.45 56.77 103.44 66.72 62.32  198106 64.05 58.06 103.35 66.81 62.4
> 198107 64.29 58.53 102.85 66.88 62.5
>     How can I modify the original program so that the second line
> (transformation) is read as characters? If I can do that,  I can continue
> working on the data after original data and transformation are read.
>
>     In the beginning I saved the transformation line in a separate file,
> but it is troublesome to contrast the two files when I need to modify the
> file. Is there any better way to work on it?

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])))

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list