[R] Add 1 week to date with hours included for subset

David Winsemius dwinsemius at comcast.net
Fri May 14 05:36:12 CEST 2010


On May 13, 2010, at 11:25 PM, emorway wrote:

>
> Forum,
>
> I've got hourly data for roughly 5 months of time that I want to  
> subset into
> successive weeks.  The first two elements of the dataset are
>
> well.80.2$date[1]
>> [1] 6/12/2000 2:00
>> 9256 Levels: 10/1/2001...
>
> well.80.2$date[2]
>> [1] 6/12/2000 3:00
>> 9256 Levels: 10/1/2001....
>
> and so on until mid-october.  I've been able to add 1 week the first  
> element
> with the following:
>
> as.POSIXlt(paste(as.character(as.Date(well.80.2$date[1],"%m/%d/%Y
> %H:%M")+7),as.character(format(strptime(well.80.2$date[1],"%m/%d/%Y
> %H:%M"),"%H:%M")),sep=" "))
>> [1] "2000-06-19 02:00:00"
>
> What I've been unable to do is then use this for comparative  
> purposes in the
> subset command, something to the effect of:
>
> contin.80.2<-subset(well.80.2,well.80.2$date>=well.80.2$date[1]) &

You simply cannot do comparisons with factor data. You need to make a  
variable that is of Date or DateTime or ti or zoo class. Then do you  
comparisons.

> well.80.2$date <
> as.POSIXlt(paste(as.character(as.Date(well.80.2$date[1],"%m/%d/%Y
> %H:%M")+7),as.character(format(strptime(well.80.2$date[1],"%m/%d/%Y
> %H:%M"),"%H:%M")),sep=" "))
>
> If there is an easier way to parse the dataset into weekly bins  
> taking into
> consideration the hours, I'm open to suggestions.
>
> Thank you.
>
> -- 
-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list