问题描述
我已阅读上一篇文章,但我无法获得我想要的内容。我需要每天16次(至少第一天和最后一天)获得一个系列,在这些情况下,间隔开始/结束与第一次/最后一次观察)。我希望观察到的变量位于相应的间隔,否则为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
谢谢!
假设你的数据框被称为数据,我请使用。他们可以很容易地使用:
#日期的转换
数据$ time< - as .POSIXct(data $ mdyhms,format =%b-%d-%Y%H:%M:%S)
#conversion到时间序列
库(xts)
TimeSeries< - xts(Data [,c(Ya,Yb)],Data [,time])
然后可以使用TimeSeries。你不能使用正常的ts,因为你没有一个常规的时间序列。没有办法在地球上保卫你的意见之间的时间间隔相同。
编辑:
您可以在评论中考虑您的评论,您可以尝试以下操作:
#计算他们进入
/ pre>
的期间#这是基于GMT和POSIXct给出从原点开始的秒数
#pass的事实。 5400是一天86400秒的1/16
数据$ mdyhms< - as.POSIXct(Data $ mdyhms,format =%b-%d-%Y%H:%M: %S,tz =GMT)
数据$ Period< - as.numeric(Data $ mdyhms)%/%5400 * 5400
#与所有的数据帧范围内的时间段
日期< - as.numeric(trunc(Data $ mdyhms,day))
nData< - data.frame(
Period = seq(min(Date),max(Date)+ 86399,by = 5400)
)
#合并两个数据帧,并取数据帧内的值的平均值
nData< - merge(Data [c('Ya','Yb','Period')],nData,by =Period,all = T)
nData< - ddply(nData, ,意思是,na.rm = T)
#制作时间序列并摆脱NaN值
#这些来自平均向量,只有NA
TS< - ts(nData [c('Ya','Yb')],frequency = 16)
TS [is.nan(TS)]< - NA
I had read previous post but I cannot obtain that I want. I need to obtain a serie with 16 intervals by day (least the first and last day, in these cases the intervals start/end with the first/last observation). I would like that the observed variables are located in the corresponding inteval and NA otherwise.
My data look as follows: [Ya and Yb are the observed variables]
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
Thanks!
解决方案Presuming your dataframe is called "Data", I'd use xts package. They're a whole lot easier to work with :
#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"])
Then TimeSeries can be used subsequently. You can't use a normal ts, because you don't have a regular time series. No way on earth you can defend that the time intervals between your observations are equal.
EDIT :
In regard of your remarks in the comments, you can try the following :
#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
这篇关于将不规则的时间序列M-D-Y hh:mm:ss转换成具有NA的常规TS填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!