2011-02-18 48 views
0

我讀過以前的文章,但我無法獲得我想要的。我需要每天以16個間隔獲得一個系列(至少第一天和最後一天,在這些情況下,間隔開始/結束於第一次/最後一次觀察)。我希望觀察到的變量位於相應的inteval中,否則NA。轉換一個不規則的時間序列M-D-Y hh:mm:ss到常規TS填充NA

我的數據如下所示:雅和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 

謝謝!

+4

你可以使用'dput`功能的對象(`dput(富)`)並複製輸出?這使我們可以更輕鬆地閱讀數據 – 2011-02-18 13:25:51

+0

感謝Marek編輯我的數據,現在我知道該怎麼做了! – Erica 2011-02-19 14:51:26

回答

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