[英]Converting zoo object into a weekly time series
I am working on building a time series in R programming language.我正在用 R 编程语言构建时间序列。 I m having a zoo object which is follows:
我有一个动物园对象,如下所示:
I 'd like to convert this into a weekly time series data for analysis and typed in the following code我想将其转换为每周时间序列数据进行分析并输入以下代码
tt2<-as.ts(zz,freq=365.25/7,start=decimal_date(ymd("2018-01-01")))
tt2[is.na(tt2)]<-0
However, I get the following output:但是,我得到以下输出:
Time Series:
Start = 17538
End = 18532
Frequency = 0.142857142857143
While I'd like to see the output in line with something like this:虽然我希望看到符合以下内容的输出:
Time Series:
Start = c(2018,2)
End = c(2020,40)
Frequency = 52
or since we can have both 53 and 52 weeks, something like:或者因为我们可以同时拥有 53 和 52 周,例如:
Time Series:
Start = 1991.0848733744
End = 2005.34360027378
Frequency = 52.1785714285714
I also tried to do the following ,我还尝试执行以下操作,
library(zoo)
zz <- read.zoo(data, split = 1, index = 2,FUN=as.week")
and converted the following into the format:并将以下内容转换为格式:
However, if i try to convert this into a time series, I receive the following output:但是,如果我尝试将其转换为时间序列,则会收到以下输出:
Time Series:
Start = 2505
End = 2647
Frequency = 1
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[40] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[79] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[118] NA NA NA NA NA NA NA NA NA 64 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
I'd be keen to receive your thoughts on this我很想收到你对此的想法
I suppose using tsibble would more easier to convert your series from daily frequency to weekly frequency.我想使用 tsibble 会更容易将您的系列从每日频率转换为每周频率。 At the end you can change to zoo object again.
最后,您可以再次更改为动物园对象。
Here is a short code on what I done这是我所做的简短代码
data数据
# A tibble: 14 x 2
Date Y
<date> <dbl>
1 2020-01-01 0.176
2 2020-01-02 0.521
3 2020-01-03 0.348
4 2020-01-04 0.801
5 2020-01-05 0.963
6 2020-01-06 0.0723
7 2020-01-07 0.638
8 2020-01-08 0.842
9 2020-01-09 0.298
10 2020-01-10 0.902
11 2020-01-11 0.943
12 2020-01-12 0.884
13 2020-01-13 0.266
14 2020-01-14 0.789
library(tsibble)
library(tidyverse)
library(zoo)
data$Date<-as.Date(data$Date)
data.w<-data%>%as_tsibble(index=Date)%>% index_by(year_week = ~ yearweek(.)) %>% summarise(weekly = sum(Y, na.rm = TRUE))
data.z<-zoo(data.w)
> data.z
year_week weekly
1 2020 W01 2.809756
2 2020 W02 4.579329
3 2020 W03 1.055690
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.