[R] Generation of missiing values in a time serie...

Gabor Grothendieck ggrothendieck at gmail.com
Tue Dec 13 14:08:40 CET 2005


Your variable mat is not a matrix; its a data frame.  Check it with:

   class(mat)

Here is an example:

x <- cbind(A = 1:4, B = 5:8)
tt <- c(1, 3:4, 6)

library(zoo)
x.zoo <- zoo(x, tt)
x.ts <- as.ts(x.zoo)


On 12/13/05, Alvaro Saurin <saurin at dcs.gla.ac.uk> wrote:
>
> Hi,
>
> First, thank you. It 'almost' works: I can convert a matrix to a
> 'zoo' object, but it can not convert it to a 'ts'.
>
> The sitruation is this: I load a set of samples with
>
>  >      h_types <- list (0, 0, character(0), character(0), 0, 0, 0, 0, 0)
>  >      h_names <- list ("time", "flow", "type", "dir", "seq", "ts", "x",
> "rtt", "size")
>  >      pcks_file       <- pipe ("grep ' P '  server.dat"), "r")
>  >      pcks            <- scan (pcks_file, what = h_types, comment.char = '#',
> fill = TRUE)
> Read 1429 records
>
>  >      mat                             <- data.frame (pcks)
>  >     colnames (mat)   <- h_names
>  >      mat
>
>            time flow type dir  seq         ts     x      rtt size
> 1      1.000893    0    P   +    0   1.000893  1472 0.000000 1472
> 2      1.514454    0    P   +    1   1.514454  2944 0.513142 1472
> 3      2.015093    0    P   +    2   2.015093  2944 0.513142 1472
> 4      2.515025    0    P   +    3   2.515025  4806 0.504488 1472
> 5      2.821976    0    P   +    4   2.821976  5730 0.496728 1472
> 6      3.078931    0    P   +    5   3.078931  5832 0.489744 1472
> 7      3.331897    0    P   +    6   3.331897  5832 0.489744 1472
>
> [...]
>
> 1425 176.925504    0    P   + 1424 176.925504 12141 0.764699 1472
> 1426 177.039489    0    P   + 1425 177.039489 12141 0.764699 1472
> 1427 177.153469    0    P   + 1426 177.153469 12141 0.764699 1472
> 1428 177.267464    0    P   + 1427 177.267464 12141 0.764699 1472
> 1429 177.381434    0    P   + 1428 177.381434 12141 0.764699 1472
>
> Then I convert it to a 'zoo', removing the 'time' column from the input
>
>  >      last_col                <- ncol (mat)
>  >      range_cols              <- 2:last_col
>  >      matrix_values   <- mat [,range_cols]
>  >      z <- zoo (matrix_values, mat $ time)
>
> So far, everything is fine. But then I need to apply a function to
> samples taken every t seconds, ie, the mean every 10 seconds. And,
> AFAIK, rapply needs a constant 'width', something that can only be
> obtained with regular time series... But if I try to get a 'ts'
> object from my 'zoo' I get
>
>  > as.ts (z)
> Error in if (del == 0 && to == 0) return(to) :
>        missing value where TRUE/FALSE needed
>
> Ummm, do you know if there is any error in my 'zoo' object? Has it
> the right format? Or, could I avoid the conversion and use the
> 'rapply' function but on time intervals (instead of points intervals)?
>
> Thank you very much.
>
> Alvaro
>
>
> On 12 Dec 2005, at 16:31, Gabor Grothendieck wrote:
>
> > First we generate some sample data x and its times tt.
> > Then using the zoo package we create an irregularly
> > spaced time series.  Now if you want a regularly spaced
> > time series convert it to ts class.  After loading zoo as
> > shown below, the R command vignette("zoo") gives more info.
> >
> > x <- 1:4
> > tt <- c(1, 3, 4, 6)
> >
> > library(zoo)
> > x.zoo <- zoo(x, tt) # irregularly spaced time series
> > x.ts <- as.ts(x.zoo) # regular time series with NAs
> >
> >
> > On 12/12/05, Alvaro Saurin <saurin at dcs.gla.ac.uk> wrote:
> >>
> >> Hi,
> >>
> >> I am a R begginer and I have a small problem with time series. I was
> >> wondering if someone could help me
> >>
> >> I am collecting data from packets going through a network, and using
> >> R for obtaining some simple statistics of connections. However, my
> >> data is not collected at a constant frequency, so I would like to
> >> create a evenly spaced TS from my traces, using the minimum time
> >> difference between two samples as the period for my new TS, and
> >> filling  the gaps with NA (or 0s).
> >>
> >> I think ther must be some simple solution for this... Anyone could
> >> help me?
> >>
> >> Thanks in advance.
> >>
> >> --
> >> Alvaro Saurin <alvaro.saurin at gmail.com> <saurin at dcs.gla.ac.uk>
> >>
> >> ______________________________________________
> >> R-help at stat.math.ethz.ch mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide! http://www.R-project.org/posting-
> >> guide.html
> >>
>
> --
> Alvaro Saurin <alvaro.saurin at gmail.com> <saurin at dcs.gla.ac.uk>
>
>
>
>




More information about the R-help mailing list