将不规则时间序列M-D-Y hh:mm:ss转换为常规TS填充NA

时间:2011-02-18 13:12:06

标签: r date time time-series

我读过上一篇文章,但我无法获得我想要的内容。我需要在白天获得一个16个区间的系列(至少在第一天和最后一天,在这些情况下,间隔从第一个/最后一个观察开始/结束)。我希望观察到的变量位于相应的inteval中,否则NA。

我的数据如下:[Ya和Yb是观察到的变量]

mdyhms                  Ya  Yb
Mar-27-2009 19:56:47    25  58.25
Mar-27-2009 20:38:59    9   81.25
Mar-28-2009 08:00:30    9   88.75
Mar-28-2009 09:26:29    0   89.25
Mar-28-2009 11:57:01    8.5 74.25
Mar-28-2009 12:19:10    7.5 71.00
Mar-28-2009 14:17:05    1.5 70.00
Mar-28-2009 15:13:14    NA  NA
Mar-28-2009 17:09:53    4   85.50
Mar-28-2009 18:37:24    0   86.00
Mar-28-2009 19:19:23    0   50.50
Mar-28-2009 20:45:50    0   36.25
Mar-29-2009 08:44:16    4.5 34.50
Mar-29-2009 10:35:12    8.5 39.50
Mar-29-2009 11:09:13    3.67    69.00
Mar-29-2009 12:40:07    0   54.25
Mar-29-2009 14:31:48    5.33    35.75
Mar-29-2009 16:19:27    6.33    71.75
Mar-29-2009 16:43:20    7.5 64.75
Mar-29-2009 18:37:42    8   83.75
Mar-29-2009 20:01:26    6.17    93.75
Mar-29-2009 20:43:53    NA  NA
Mar-30-2009 08:42:05    12.67   88.50
Mar-30-2009 09:52:57    4.33    75.50
Mar-30-2009 12:01:32    1.83    70.75
Mar-30-2009 12:19:40    NA  NA
Mar-30-2009 14:23:37    3.83    86.75
Mar-30-2009 16:00:59    37.33   80.25
Mar-30-2009 17:19:28    10.17   77.75
Mar-30-2009 17:49:12    9.83    73.00
Mar-30-2009 20:06:00    11.17   76.75
Mar-30-2009 21:40:35    20.33   68.25
Mar-31-2009 08:11:12    18.33   69.75
Mar-31-2009 09:51:29    14.5    65.50
Mar-31-2009 11:10:41    NA  NA
Mar-31-2009 13:27:09    NA  NA
Mar-31-2009 13:44:35    NA  NA
Mar-31-2009 16:01:23    NA  NA
Mar-31-2009 16:56:14    NA  NA
Mar-31-2009 18:27:28    NA  NA
Mar-31-2009 19:17:46    NA  NA
Mar-31-2009 21:12:22    NA  NA
Apr-01-2009 08:35:24    2.33    60.25
Apr-01-2009 09:24:49    1.33    71.50
Apr-01-2009 11:28:34    5.67    62.00
Apr-01-2009 13:31:48    NA  NA
Apr-01-2009 14:52:18    NA  NA
Apr-01-2009 15:11:44    1.5 71.50
Apr-01-2009 17:00:53    3.17    84.00

谢谢!

1 个答案:

答案 0 :(得分:4)

假设您的数据框称为“数据”,我会使用xts package。他们使用起来更容易:

#Conversion of dates
Data$time <- as.POSIXct(Data$mdyhms,format="%b-%d-%Y %H:%M:%S")

#conversion to time series
library(xts)
TimeSeries <- xts(Data[,c("Ya","Yb")],Data[,"time"])

然后可以使用TimeSeries。您不能使用普通的ts,因为您没有常规时间序列。在地球上你无法保证观察之间的时间间隔相等。

编辑:

关于您在评论中的评论,您可以尝试以下方法:

#Calculate the period they're into
#This is based on GMT and the fact that POSIXct gives the number of seconds
#passed since the origin. 5400 is 1/16 of 86400 seconds in a day

Data$mdyhms <- as.POSIXct(Data$mdyhms,format="%b-%d-%Y %H:%M:%S",tz="GMT")
Data$Period <- as.numeric(Data$mdyhms) %/% 5400 * 5400

#Make a new data frame with all periods in the range of the dataframe

Date <- as.numeric(trunc(Data$mdyhms,"day"))
nData <- data.frame(
    Period = seq(min(Date),max(Date)+86399,by=5400)
)
# Merge both dataframes and take the mean of values within a dataframe

nData <- merge(Data[c('Ya','Yb','Period')],nData,by="Period",all=T)
nData <- ddply(nData,"Period",mean,na.rm=T)

#Make the time series and get rid of the NaN values
#These come from averaging vectors with only NA
TS <- ts(nData[c('Ya','Yb')],frequency=16)
TS[is.nan(TS)] <- NA