Hi, Does anyone know how to add minutes (up to 100 min) to a 24 hour time, to create a new 24 hour time? I can't seem to find any documentation or examples explaining how to do this. The variables of interest are 'ARRIVE','WAIT', and 'DEPART' in the attached partial dataframe. I want 'DEPART' to be the "sum" of 'ARRIVE' and 'WAIT' in 24 hour format. Also, can anyone direct me to some relevant documentation? Thank you, Mike -------------- next part -------------- A non-text attachment was scrubbed... Name: Dataframe.pdf Type: application/pdf Size: 81702 bytes Desc: Dataframe.pdf URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100317/8f86757f/attachment.pdf>
Hi r-help-bounces at r-project.org napsal dne 17.03.2010 14:57:32:> Hi, > > Does anyone know how to add minutes (up to 100 min) to a 24 hour time,to> create a new 24 hour time? I can't seem to find any documentation orexamples> explaining how to do this. The variables of interest are'ARRIVE','WAIT', and> 'DEPART' in the attached partial dataframe. I want 'DEPART' to be the"sum" of> 'ARRIVE' and 'WAIT' in 24 hour format. Also, can anyone direct me tosome> relevant documentation?You can make a date/time object from YEAR, WEEK, ARRIVE by mydate <- strptime(paste(....)) see ?strptime then you can add ARRIVE just by mydate+ARRIVE*60 (You did not say explicitly what values are ARRIVE so assume minutes) You shall probably also consult zoo library, especially na.locf function. Regards Petr> > Thank you, > > Mike > [p??loha Dataframe.pdf odstran?na u?ivatelem Petr PIKAL/CTCAP] > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
On Wed, Mar 17, 2010 at 2:57 PM, Hosack, Michael <mhosack at state.pa.us> wrote:> Hi, > > Does anyone know how to add minutes (up to 100 min) to a 24 hour time, to create a new 24 hour time? I can't seem to find any documentation or examples explaining how to do this. The variables of interest are 'ARRIVE','WAIT', and 'DEPART' in the attached partial dataframe. I want 'DEPART' to be the "sum" of 'ARRIVE' and 'WAIT' in 24 hour format. Also, can anyone direct me to some relevant documentation? > > Thank you, > > Mike >If you convert all data to a date-and-time ?POSIXlt object, you can just convert the minutes to seconds and add together with "+". Another way would be the something like this: addTime<-function(timeTxt,mins){ start.time<-strsplit(timeTxt,":") start.time<-do.call("rbind",start.time) storage.mode(start.time)<-"numeric" hours<-mins%/%60 mins.left<-mins%%60 end.mins<-(start.time[,2]+mins.left)%%60 end.hours<-(start.time[,1]+hours+(start.time[,2]+mins.left)%/%60)%%24 end.time<-paste(end.hours,end.mins,sep=":") return(end.time) } addTime(c("15:23","7:00"),c(70,100)) or this: addTime2<-function(timeTxt,mins){ orig.date<-as.POSIXct(paste("2001-01-01",timeTxt)) new.Date<-orig.date+mins*60 new.Date<-strsplit(as.character(new.Date)," ") new.Time<-(sapply(new.Date,"[",2)) return(new.Time) } addTime2(c("15:23","7:00"),c(70,100)) Regards, Gustaf -- Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik