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]) & 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. -- View this message in context: http://r.789695.n4.nabble.com/Add-1-week-to-date-with-hours-included-for-subset-tp2216062p2216062.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2010-May-14 03:36 UTC
[R] Add 1 week to date with hours included for subset
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
For those whose search lands on this post, here is the solution I found that worked well.80.2<-data.frame(well.80.2,week=floor(as.numeric(difftime(strptime(well.80.2$date,"%m/%d/%Y %H:%M",tz=""),strptime(well.80.2$date[1],"%m/%d/%Y %H:%M",tz=""),tz="",units="hours"))/168)) -- View this message in context: http://r.789695.n4.nabble.com/Add-1-week-to-date-with-hours-included-for-subset-tp2216062p2216710.html Sent from the R help mailing list archive at Nabble.com.