Skip to content

use zoo package with multiple column data sets

6 messages · Gabor Grothendieck, e-letter

#
Readers,

I am trying to use the zoo package with an array of data:

file1:
hh:mm:ss	1
hh:mm:ss	2
hh:mm:ss	3
hh:mm:ss	4

file2:
hh:mm:ss	11	55
hh:mm:ss	22	66
hh:mm:ss	33	77
hh:mm:ss	44	88

I wanted to merge these data set so I tried the following commands:

library(chron)
library(zoo)
z1<-read.zoo("path/to/file1.csv",header=TRUE,sep=",",FUN=times)
z2<-read.zoo("path/to/file2.csv",header=TRUE,sep=",",FUN=times)
z3<-(na.approx(merge(z1,z2),time(z1)))
plot(z3$z1,z3$z2[3])

Warning message:
some methods for ?zoo? objects do not work if the index entries in
?order.by? are not unique in: zoo(rval, ix)
Error in merge.zoo(z1, z2) : series cannot be merged with non-unique
index entries in a series

I have read the document vignette 'zoo' but section 2.1 does not show
an example of the syntax for the command 'order.by'.

How to use the zoo package where the variable z2 contains many columns of data?

Yours,

rhelpatconference.jabber.org
r251
#
See FAQ #1 of the zoo faq:

vignette("zoo-faq")
On Fri, Jan 29, 2010 at 7:12 AM, e-letter <inpost at gmail.com> wrote:
#
Assuming my documentation is correct, my version shows faq 1 to refer
to duplicate times but if file2 is:

01:01:01        11      55
01:01:04        22      66
01:01:07        33      77
01:01:10        44      88

I cannot see what is duplicate? If I create two new files:

file3:
01:01:01        11
01:01:04        22
01:01:07        33
01:01:10        44

file4:
01:01:01        55
01:01:04        66
01:01:07        77
01:01:10        88

The previous commands work:

z1<-read.zoo("path/to/file1.csv",header=TRUE,sep=",",FUN=times)
z2<-read.zoo("path/to/file3.csv",header=TRUE,sep=",",FUN=times)
z3<-(na.approx(merge(z1,z2),time(z1)))
plot(z3$z1,z3$z2)

and:

z1<-read.zoo("path/to/file1.csv",header=TRUE,sep=",",FUN=times)
z2<-read.zoo("path/to/file4.csv",header=TRUE,sep=",",FUN=times)
z3<-(na.approx(merge(z1,z2),time(z1)))
plot(z3$z1,z3$z2)

Shouldn't I be able to have one file containing all the columns I want
to make graphs, instead of having to create numerous files of only two
columns of data?
#
Please provide a reproducible examples.  You can use the following style:
+ 01:01:04        22      66
+ 01:01:07        33      77
+ 01:01:10        44      88"
V2 V3
01:01:01 11 55
01:01:04 22 66
01:01:07 33 77
01:01:10 44 88
On Fri, Jan 29, 2010 at 8:34 AM, e-letter <inpost at gmail.com> wrote:
#
Error in approx(along[!na], y[!na], along[na], ...) :
        need at least two non-NA values to interpolate
Error in approx(along[!na], y[!na], along[na], ...) :
        need at least two non-NA values to interpolate
Error in `[.zoo`(z2, , 3) : subscript out of bounds
Error in textConnection(z2) : invalid 'text' argument
#
Its a bug.  Its now fixed it in the development source so try this:

library(chron)

library(zoo)
source("http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/*checkout*/pkg/zoo/R/na.approx.R?rev=653&root=zoo")
source("http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/*checkout*/pkg/zoo/R/zoo.R?rev=653&root=zoo")

z1 <-
structure(1:4, index = structure(c(0.0423611111111111, 0.0428819444444444,
0.0434027777777778, 0.0439236111111111), format = "h:m:s", class =
"times"), class = "zoo")

z2 <-
structure(c(11L, 22L, 33L, 44L, 55L, 66L, 77L, 88L), .Dim = c(4L,
2L), .Dimnames = list(NULL, c("data", "data.1")), index =
structure(c(0.0423726851851852,
0.0424074074074074, 0.0424421296296296, 0.0424768518518518), format =
"h:m:s", class = "times"), class = "zoo")

out1 <- na.approx(merge(z1, z2), time(z1)); out1
out2 <- na.approx(merge(z1, z2), time(z1), na.rm = FALSE); out2
out3 <- na.approx(merge(z1, z2), time(z1), rule = 2); out3
out4 <- na.approx(merge(z1, z2)); out4
out5 <- na.approx(merge(z1, z2), na.rm = FALSE); out5
out6 <- na.approx(merge(z1, z2), rule = 2); out6

The above style (which uses the output of dput to form z1 and z2) or
the style requested in my last response using textConnection allows
one to copy and paste directly into one's session (rather than having
to download two files, copying and pasting to an editor, editing the
paths and finally copying and pasting to R).
On Sat, Jan 30, 2010 at 6:08 AM, e-letter <inpost at gmail.com> wrote: