R 时间序列周数据

R 时间序列周数据,r,time-series,R,Time Series,我对R非常陌生,在将数据作为csv文件移动到R之前,我尝试在excel中操作数据。我想对每周数据使用ts()。你能做一些简单的事情,比如一年中第1-12个月的第1-4周吗?嗯。我在想,如果我用第1-7天作为第一周等等,这对我来说会很划一,也很容易,但我不知道怎么写。使用此站点和另一个站点作为教程,我得出以下结论: myts <- ts(Time2012, start = c(8/3/2013,1), end = c(9/2/2013,4), frequency = 52) myts我建议

我对R非常陌生,在将数据作为csv文件移动到R之前,我尝试在excel中操作数据。我想对每周数据使用
ts()
。你能做一些简单的事情,比如一年中第1-12个月的第1-4周吗?嗯。我在想,如果我用第1-7天作为第一周等等,这对我来说会很划一,也很容易,但我不知道怎么写。使用此站点和另一个站点作为教程,我得出以下结论:

myts <- ts(Time2012, start = c(8/3/2013,1), end = c(9/2/2013,4), frequency = 52)

myts我建议使用稍微不同的工作流,您可能会发现它有更广泛的实用性:

> end = Sys.Date()
> start = end - 365

> class

> # create the index array comprised of date objects
> ndx = seq(start, end, by='weeks')
> class(ndx)
  [1] "Date"
> length(ndx)
  [1] 53

> # create a fake data array
> x = 1:length(ndx)
> mydata = sin(x/2)

> # import a time series library 
> require(xts)

> # create the time series
> myts = xts(mydata, order.by=ndx)

> myts[1:5]
               [,1]
  2012-09-19 3.479426
  2012-09-26 3.841471
  2012-10-03 3.997495
  2012-10-10 3.909297
  2012-10-17 3.598472

> class(myts)
  [1] "xts" "zoo"

> periodicity(myts)
  Weekly periodicity from 2012-09-19 to 2013-09-18 
或者,如果您的数据不是按周计算的,则可以创建具有更高分辨率(例如,天)的时间序列,然后将其上卷到周:

> ndx = seq(start, end, by='days')

> x = 1:length(ndx)
> mydata = sin(x/2) + 3
> myts = xts(mydata, order.by=ndx)

> myts[1:5]  
             [,1]
2012-09-19 3.479426
2012-09-20 3.841471
2012-09-21 3.997495
2012-09-22 3.909297
2012-09-23 3.598472

> periodicity(myts)
    Daily periodicity from 2012-09-19 to 2013-09-19 

> # now roll-up this daily series to weeks

> require(xts)

> # first create the endpoints
> np = endpoints(myts, on='weeks')


> myts_weeks = period.apply(x=myts, INDEX=np, FUN=sum, na.rm=TRUE)
> myts_weeks[1:5]
               [,1]
  2012-09-23 18.82616
  2012-09-30 17.11212
  2012-10-07 24.93492
  2012-10-14 17.51811
  2012-10-21 23.58635

> periodicity(myts_weeks)
  Weekly periodicity from 2012-09-23 to 2013-09-19 

我建议您使用稍有不同的工作流,您可能会发现该工作流具有更广泛的实用性:

> end = Sys.Date()
> start = end - 365

> class

> # create the index array comprised of date objects
> ndx = seq(start, end, by='weeks')
> class(ndx)
  [1] "Date"
> length(ndx)
  [1] 53

> # create a fake data array
> x = 1:length(ndx)
> mydata = sin(x/2)

> # import a time series library 
> require(xts)

> # create the time series
> myts = xts(mydata, order.by=ndx)

> myts[1:5]
               [,1]
  2012-09-19 3.479426
  2012-09-26 3.841471
  2012-10-03 3.997495
  2012-10-10 3.909297
  2012-10-17 3.598472

> class(myts)
  [1] "xts" "zoo"

> periodicity(myts)
  Weekly periodicity from 2012-09-19 to 2013-09-18 
或者,如果您的数据不是按周计算的,则可以创建具有更高分辨率(例如,天)的时间序列,然后将其上卷到周:

> ndx = seq(start, end, by='days')

> x = 1:length(ndx)
> mydata = sin(x/2) + 3
> myts = xts(mydata, order.by=ndx)

> myts[1:5]  
             [,1]
2012-09-19 3.479426
2012-09-20 3.841471
2012-09-21 3.997495
2012-09-22 3.909297
2012-09-23 3.598472

> periodicity(myts)
    Daily periodicity from 2012-09-19 to 2013-09-19 

> # now roll-up this daily series to weeks

> require(xts)

> # first create the endpoints
> np = endpoints(myts, on='weeks')


> myts_weeks = period.apply(x=myts, INDEX=np, FUN=sum, na.rm=TRUE)
> myts_weeks[1:5]
               [,1]
  2012-09-23 18.82616
  2012-09-30 17.11212
  2012-10-07 24.93492
  2012-10-14 17.51811
  2012-10-21 23.58635

> periodicity(myts_weeks)
  Weekly periodicity from 2012-09-23 to 2013-09-19 

使用
lubridate
创建一组年、月、周(月)的简单方法

require(lubridate)

# Your starting date, plus 52 more dates at weekly intervals
xDates <- dmy("8/3/2013") + weeks(0:52)

# A data frame of the dates, the month of the year, and the week of the month
xYMW <- data.frame(date=(xDates), month=month(xDates), week=mday(xDates) %/% 7 + 1)
xYMW[1:5, ]
        date month week
1 2013-03-08     3    2
2 2013-03-15     3    3
3 2013-03-22     3    4
4 2013-03-29     3    5
5 2013-04-05     4    1
require(润滑油)
#你的开始日期,再加上每周52次的约会

xDates创建一组年、月、周(月)的更简单方法是使用
lubridate

require(lubridate)

# Your starting date, plus 52 more dates at weekly intervals
xDates <- dmy("8/3/2013") + weeks(0:52)

# A data frame of the dates, the month of the year, and the week of the month
xYMW <- data.frame(date=(xDates), month=month(xDates), week=mday(xDates) %/% 7 + 1)
xYMW[1:5, ]
        date month week
1 2013-03-08     3    2
2 2013-03-15     3    3
3 2013-03-22     3    4
4 2013-03-29     3    5
5 2013-04-05     4    1
require(润滑油)
#你的开始日期,再加上每周52次的约会

现在还不清楚你在问什么。你应该给出你的输入和期望的输出的样本(可复制的例子)。除了提供一个例子,你应该更好地描述你的问题。你的目标是生产什么?为什么一个
ts
对象而不是(比如)一个
zoo
对象?也许一个简单的数据框架会更好-请更多的上下文。也请阅读和。谢谢。在我的水晶球里,我看到你有一个三栏的电子表格,年、月、周。如果是这样的话,我祝你好运,因为R在解释缺少信息的日期时很糟糕(例如,如果你至少不知道年份和日期)@SlowLeaner关于
zoo
或数据帧的建议可能会有所帮助
zoo
了解不规则的时间序列(例如,第1周不在第5周后的7天之后),但是如果您需要一个
ts
对象来馈送,例如,
forecast
,这将是一个棘手的问题。尝试在Excel中将YMD转换为一个假装的yyyy/mm/dd,然后查看
lubridate
。不清楚您在问什么。你应该给出你的输入和期望的输出的样本(可复制的例子)。除了提供一个例子,你应该更好地描述你的问题。你的目标是生产什么?为什么一个
ts
对象而不是(比如)一个
zoo
对象?也许一个简单的数据框架会更好-请更多的上下文。也请阅读和。谢谢。在我的水晶球里,我看到你有一个三栏的电子表格,年、月、周。如果是这样的话,我祝你好运,因为R在解释缺少信息的日期时很糟糕(例如,如果你至少不知道年份和日期)@SlowLeaner关于
zoo
或数据帧的建议可能会有所帮助
zoo
了解不规则的时间序列(例如,第1周不在第5周后的7天之后),但是如果您需要一个
ts
对象来馈送,例如,
forecast
,这将是一个棘手的问题。尝试在Excel中将YMD转换为一个模拟的yyyy/mm/dd,然后查看
lubridate