[英]R: How to extract dates from a time series
如何從時間序列中提取日期? 這是一個時間序列:
x = seq (1, 768)
myts <- ts(x, start=1982, frequency=24)
最初我需要為rts
功能創建一個保存日期/時間數據的向量,觀察始於1982年,每月進行2次測量直至2013年。
嘗試:
time(myts)
也許:
library(zoo)
as.yearmon(time(myts))
如果您需要POSIX*
對象-與半月度數據時,但具有較高的時間分辨率打交道時可能會派上用場這可能是不恰當的-你也可以使用date_decimal
從lubridate。
library(lubridate)
mts <- as.numeric(time(myts))
## 'POSIXct, POSIXt' object
tms <- date_decimal(mts)
您可以使用以下功能。 輸入是r中的時間序列 。 輸出是一個包含從開始時間到結束時間的所有時間數組的列表 。
在列表的幫助下,您可以使用window()
函數非常方便地截斷時間序列。
getTStime <- function(ats){
start <- start(ats)
end <- end(ats)
time <- list()
time[[1]] <- start
m <- 2
while(!(identical(start, end))){
start[2] <- start[2] + 1
if (start[2]==13){
start[1] <- start[1] + 1
start[2] <- 1
}
time[[m]] <- start
m <- m + 1
}
return(time)
}
問題中給出的原始問題是非常困難的,因為它包含非標准抽樣,即每年24次,因此您必須使用非標准方法來提取其他人已充分涵蓋的日期。 對於那些偶然發現具有更多標准時間序列的問題的人來說,解決方案更簡單。
x <- 1:(768/2) ## shorten the ts to reflect lower sampling rate
myts <- ts(x, start = c(1982, 1), frequency = 12) ## now one sample per month
dates <- as.Date(myts)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.