Coercion from and to zoo
as.zoo.RdMethods for coercing "zoo" objects to other classes and
a generic function as.zoo for coercing objects to class "zoo".
Arguments
- x
an object,
- ...
further arguments passed to
zoowhen the return object is created.
Details
as.zoo currently has a default method and methods for ts,
fts (currently archived on CRAN), irts,
mcmc, tis, xts
objects (and zoo objects themselves).
Methods for coercing objects of class "zoo" to other classes
currently include: as.ts, as.matrix, as.vector,
as.data.frame, as.list (the latter also being available
for "ts" objects). Furthermore, fortify.zoo can transform "zoo"
series to "data.frame" including the time index and optionally melting a wide series
into a long data frame.
In the conversion between zoo and ts, the zooreg class is
always used.
Value
as.zoo returns a zoo object.
Examples
suppressWarnings(RNGversion("3.5.0"))
set.seed(1)
## coercion to zoo:
## default method
as.zoo(rnorm(5))
#> 1 2 3 4 5
#> -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078
## method for "ts" objects
as.zoo(ts(rnorm(5), start = 1981, freq = 12))
#> Jan 1981 Feb 1981 Mar 1981 Apr 1981 May 1981
#> -0.8204684 0.4874291 0.7383247 0.5757814 -0.3053884
## coercion from zoo:
x.date <- as.POSIXct(paste("2003-", rep(1:4, 4:1), "-", sample(1:28, 10, replace = TRUE), sep = ""))
x <- zoo(matrix(rnorm(24), ncol = 2), x.date)
as.matrix(x)
#> x.1 x.2
#> 2003-01-04 0.82122120 1.35867955
#> 2003-01-06 -0.01619026 -0.47815006
#> 2003-01-19 0.94383621 0.41794156
#> 2003-01-27 -0.04493361 -1.47075238
#> 2003-02-01 0.78213630 -0.05380504
#> 2003-02-08 0.59390132 -0.10278773
#> 2003-02-11 0.91897737 0.38767161
#> 2003-03-11 0.07456498 -1.37705956
#> 2003-03-25 -1.98935170 -0.41499456
#> 2003-04-10 0.61982575 -0.39428995
as.vector(x)
#> [1] 0.82122120 -0.01619026 0.94383621 -0.04493361 0.78213630 0.59390132
#> [7] 0.91897737 0.07456498 -1.98935170 0.61982575 1.35867955 -0.47815006
#> [13] 0.41794156 -1.47075238 -0.05380504 -0.10278773 0.38767161 -1.37705956
#> [19] -0.41499456 -0.39428995
as.data.frame(x)
#> x.1 x.2
#> 2003-01-04 0.82122120 1.35867955
#> 2003-01-06 -0.01619026 -0.47815006
#> 2003-01-19 0.94383621 0.41794156
#> 2003-01-27 -0.04493361 -1.47075238
#> 2003-02-01 0.78213630 -0.05380504
#> 2003-02-08 0.59390132 -0.10278773
#> 2003-02-11 0.91897737 0.38767161
#> 2003-03-11 0.07456498 -1.37705956
#> 2003-03-25 -1.98935170 -0.41499456
#> 2003-04-10 0.61982575 -0.39428995
as.list(x)
#> $x.1
#> 2003-01-04 2003-01-06 2003-01-19 2003-01-27 2003-02-01 2003-02-08
#> 0.82122120 -0.01619026 0.94383621 -0.04493361 0.78213630 0.59390132
#> 2003-02-11 2003-03-11 2003-03-25 2003-04-10
#> 0.91897737 0.07456498 -1.98935170 0.61982575
#>
#> $x.2
#> 2003-01-04 2003-01-06 2003-01-19 2003-01-27 2003-02-01 2003-02-08
#> 1.35867955 -0.47815006 0.41794156 -1.47075238 -0.05380504 -0.10278773
#> 2003-02-11 2003-03-11 2003-03-25 2003-04-10
#> 0.38767161 -1.37705956 -0.41499456 -0.39428995
#>