R 如何将具有POSIXct日期的数据帧转换为时间序列?

R 如何将具有POSIXct日期的数据帧转换为时间序列?,r,time-series,lubridate,R,Time Series,Lubridate,我有一个数据框(DF),有两列。在第一栏我有日期,在第二栏我有我的兴趣值(VOI) DF的显示如下: |---------------------|------------------| | Date | VOI | |---------------------|------------------| | Jan-1971 | 34 | |---------------------|----

我有一个数据框(DF),有两列。在第一栏我有日期,在第二栏我有我的兴趣值(VOI)

DF的显示如下:

|---------------------|------------------|
|        Date         |        VOI       |
|---------------------|------------------|
|          Jan-1971   |         34       |
|---------------------|------------------|
|          Jan-1972   |         28       |
|---------------------|------------------|
|          Jan-1973   |         29       |
|---------------------|------------------|
|          Jan-1974   |         37       |
|---------------------|------------------|
|             ...     |         ...      |
|---------------------|------------------|
|          Jan-2017   |         36       |
|---------------------|------------------|
|          Fev-1971   |         48       |
|---------------------|------------------|
|          Fev-1972   |         49       |
|---------------------|------------------|
|          Fev-1973   |         52       |
|---------------------|------------------|
|          Fev-1974   |         50       |
|---------------------|------------------|
|          ...        |         ...      |
|---------------------|------------------|
|          Mar-1971   |         30       |
|---------------------|------------------|
|          ...        |         ...      |
|---------------------|------------------|
|          Mar-2017   |         36       |
|---------------------|------------------|
|          ...        |         ...      |
|---------------------|------------------|
|          Dez-1971   |         15       |
|---------------------|------------------|
|          ...        |        ...       |
|---------------------|------------------|
|          Dez-2017   |         19       |
|---------------------|------------------|
简言之,数据以月为一个聚合周期呈现

首先,我有1971年至2017年1月的所有VOI(47个数据点),然后我有同一时期2月的所有VOI,因此,分数相同。这种重复持续到12月,也有47个数据点

我应用了lubridate中的ymd()将日期转换为POSIXct值

现在我想用我的VOI创建一个时间序列对象。我试过:

ts = xts(x = df$Vazao, order.by = index(df$Date))

但都不管用。我不知道我在哪里犯了错误,但我想这和我的约会没有按时间顺序来有什么关系。我认为使用ymd()命令可以解决这个问题,并“让R了解”我的时间序列是从1971年1月、1971年2月、1971年3月、2017年12月开始的

如何将此数据帧转换为时间序列对象


谢谢你的意见。

这就是你想要的吗

首先,整理一些数据

y <- 1971:2017
length(ano)
m <- seq(as.Date("2017-01-01"), as.Date("2017-12-31"), by = 28)
m <- unique(format(m, "%b"))
Date <- expand.grid(y, m)[2:1]
Date <- apply(Date, 1, paste, collapse = "-")
DF <- data.frame(Date = date, VOI = sample(100, length(date), TRUE))
head(DF)
#      Date VOI
#1 Jan-1971  12
#2 Jan-1972  89
#3 Jan-1973  99
#4 Jan-1974  77
#5 Jan-1975   5
#6 Jan-1976  46

由于您的
Date
只获得了日期的
month
year
,因此,您可以使用
zoo::yearmon
函数将
Date
转换为类
yearmon
,这是
xts
函数可以接受的

解释了
xts
order.by
参数的期望值 在帮助中:

xts对象从相同的包扩展S3类zoo 名字

此扩展中的第一个不同之处是提供了以下要求: 索引值不仅是唯一的和有序的,而且必须是 基于时间的课程当前可接受的类别包括:
日期
POSIXct
timeDate
,以及
yearmon
yearqtr
,其中 索引值保持唯一

解决方案可以是:

# Sample data. This data will have Date in `Jan-1971` format.
# Data has been created only for 36 months.  
set.seed(1)
df <- data.frame( Date = format(seq(as.Date("1971-01-01"), 
                     as.Date("1973-12-31"), by="month"), "%b-%Y"),
            VOI = as.integer(runif(36)*100), stringsAsFactors = FALSE)


library(zoo)    
library(xts)


#Convert Date column to type `yearmon`
ts = xts(x = df$VOI, order.by = as.yearmon(df$Date, "%b-%Y"))

head(ts)
# [,1]
# Jan 1971   26
# Feb 1971   37
# Mar 1971   57
# Apr 1971   90
# May 1971   20
# Jun 1971   89
#示例数据。该数据的日期为“1971年1月”格式。
#数据仅创建了36个月。
种子(1)

df您应该输出一个数据样本。
library(xts)

ts <- xts(DF[, "VOI"], order.by = as.Date(paste0("01-", DF$Date), "%d-%b-%Y"))

str(ts)
#An ‘xts’ object on 1971-01-01/2017-12-01 containing:
#  Data: int [1:564, 1] 76 90 7 61 3 49 1 19 51 90 ...
#  Indexed by objects of class: [Date] TZ: UTC
#  xts Attributes:  
# NULL


head(ts)
           [,1]
#1971-01-01   76
#1971-02-01   90
#1971-03-01    7
#1971-04-01   61
#1971-05-01    3
#1971-06-01   49
# Sample data. This data will have Date in `Jan-1971` format.
# Data has been created only for 36 months.  
set.seed(1)
df <- data.frame( Date = format(seq(as.Date("1971-01-01"), 
                     as.Date("1973-12-31"), by="month"), "%b-%Y"),
            VOI = as.integer(runif(36)*100), stringsAsFactors = FALSE)


library(zoo)    
library(xts)


#Convert Date column to type `yearmon`
ts = xts(x = df$VOI, order.by = as.yearmon(df$Date, "%b-%Y"))

head(ts)
# [,1]
# Jan 1971   26
# Feb 1971   37
# Mar 1971   57
# Apr 1971   90
# May 1971   20
# Jun 1971   89