[R] Date period

David Winsemius dwinsemius at comcast.net
Wed Aug 1 19:16:28 CEST 2012


On Aug 1, 2012, at 12:37 AM, Pascal Oettli wrote:

> Hello,
>
> You can try the following:
>
> x <- read.table(text="2000-01-05 00:00:00           1.0
> 2000-01-05 01:00:00           1.0
> 2000-01-05 05:00:00           3.6
> 2000-01-05 06:00:00           3.6
> 2000-01-05 07:00:00           2.2
> 2000-01-05 08:00:00           2.2
> 2000-01-05 09:00:00           2.2
> 2000-01-05 10:00:00           2.2
> 2000-01-05 11:00:00           2.2
> 2000-02-05 00:00:00           1.0
> 2000-02-05 01:00:00           1.0
> 2000-02-05 05:00:00           3.6
> 2000-02-05 06:00:00           3.6
> 2000-02-05 07:00:00           2.2
> 2000-02-05 08:00:00           2.2
> 2000-02-05 09:00:00           2.2
> 2000-02-05 10:00:00           2.2
> 2000-02-05 11:00:00           2.2")
>
> colnames(x) <- c('Date','Time','Value')
>
> y <-  
> c('00:00:00','01:00:00','02:00:00','03:00:00','04:00:00','05:00:00')

Asking the the programmer enumerate all elements in a range may be  
rather  onerous. Why not compare the values using string comparisons,  
since they are in a regular format and the leading zeroes will mean  
that the times will be sorted properly? First, do the input with  
stringsAsFactors=FALSE, then:

trange <- c( "00:00:00",  "05:00:00")
x[ x$Time >= trange[1] & x$Time <= trange[2] , ]
          Date     Time Value
1  2000-01-05 00:00:00   1.0
2  2000-01-05 01:00:00   1.0
3  2000-01-05 05:00:00   3.6
10 2000-02-05 00:00:00   1.0
11 2000-02-05 01:00:00   1.0
12 2000-02-05 05:00:00   3.6


Given the fact that they are described as a "time series" I wonder if  
the OP forgot to offer dput() on the example, and we might find that  
it was already a zoo or an xts object. If so, this discussion may not  
apply to his data situation.

-- 
David.
>
> idx <- is.element(x$Time, y)
> x[idx,]
>
> Hope this help.
> Pascal
>
>
> Le 01/08/2012 15:48, Васильченко Александр a  
> écrit :
>> Hello
>> I have dataframe
>> How to remove dates in hourly time seriesThe example time series  
>> likedate
>>                                value
>> 2000-01-05 00:00:00           1.0
>> 2000-01-05 01:00:00           1.0
>> 2000-01-05 05:00:00           3.6
>> 2000-01-05 06:00:00           3.6
>> 2000-01-05 07:00:00           2.2
>> 2000-01-05 08:00:00           2.2
>> 2000-01-05 09:00:00           2.2
>> 2000-01-05 10:00:00           2.2
>> 2000-01-05 11:00:00           2.2
>> 2000-02-05 00:00:00           1.0
>> 2000-02-05 01:00:00           1.0
>> 2000-02-05 05:00:00           3.6
>> 2000-02-05 06:00:00           3.6
>> 2000-02-05 07:00:00           2.2
>> 2000-02-05 08:00:00           2.2
>> 2000-02-05 09:00:00           2.2
>> 2000-02-05 10:00:00           2.2
>> 2000-02-05 11:00:00           2.2
>>
>> And I take interval for example from 00:00:00 to 05:00:00.
>> date                                  value
>> 2000-01-05 00:00:00           1.0
>> 2000-01-05 01:00:00           1.0
>> 2000-01-05 05:00:00           3.6
>> 2000-02-05 00:00:00           1.0
>> 2000-02-05 01:00:00           1.0
>> 2000-02-05 05:00:00           3.6
>> Regards, Aleksander.
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Alameda, CA, USA



More information about the R-help mailing list