[英]How can I transform a dataframe with POSIXct dates into a time series?
我有一個有兩列的數據框(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))
和
ts = xts(x = df$Vazao, order.by = df$Data)
但都沒有用。 我不知道我在哪里犯了一個錯誤,但我想知道這與我的日期不是按時間順序排列有關。 我認為使用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
現在,只需將函數xts
與適當的參數一起應用即可。 請注意,您的Date
列沒有day
值,所以我必須要paste
一個。 第01
天總是一個不錯的選擇。
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
因為,你的Date
只有month
和year
的日期,你可以使用zoo::yearmon
函數將Date
轉換為xts
函數可接受的類yearmon
。
xts
的order.by
參數的期望在幫助中解釋為:xts對象從同名的包中擴展S3類zoo。
此擴展的第一個區別是要求索引值不僅是唯一的和有序的,而且必須是基於時間的類。 目前可接受的類包括:
Date
,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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.