将动物园对象转换为每周时间序列 [英] Converting zoo object into a weekly time series

查看:136
本文介绍了将动物园对象转换为每周时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R编程语言构建时间序列.我有一个动物园物体,如下所示:

I am working on building a time series in R programming language. 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

或者因为我们可以同时拥有53和52个星期,所以类似:

or since we can have both 53 and 52 weeks, something like:

Time Series:
Start = 1991.0848733744 
End = 2005.34360027378 
Frequency = 52.1785714285714 

我还尝试了以下操作,

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

推荐答案

我想使用tsibble可以更轻松地将您的系列从每日频率转换为每周频率.最后,您可以再次更改为动物园对象.

I suppose using tsibble would more easier to convert your series from daily frequency to weekly frequency. At the end you can change to zoo object again.

这是我所做的简短代码

数据

# 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

这篇关于将动物园对象转换为每周时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆