Thanks Jim and David, The difftime() is exactly what I am looking for. Just out of curiosity why the unit of output is different. here is an example of the dataframe.
test<-structure(list(SPDTC = c("2012-08-27T09:30", "2012-08-06T10:08", "2012-08-13T07:41", "2012-07-17T07:50", "2012-09-11T10:29"), TimeBL = c("2012-07-17T07:50", "2012-07-17T07:50", "2012-07-17T07:50", "2012-07-17T07:50", "2012-07-17T07:50")), .Names = c("SPDTC", "TimeBL"), row.names = c(NA, 5L), class = "data.frame") Here is the calculation I did originally. >with(test,strptime(SPDTC,format='%Y-%m-%dT%H:%M')-strptime(TimeBL,format='%Y-%m-%dT%H:%M')) The result is in seconds. On Fri, Aug 9, 2013 at 1:55 PM, David Winsemius <dwinsem...@comcast.net>wrote: > > On Aug 9, 2013, at 10:37 AM, Jun Shen wrote: > > > Hi all, > > > > I used strptime() to convert character strings to time and did some > > subtraction calculation. > > > > a<-'2012-07-17T07:50' > > b<-'2012-08-27T09:30' > > > > strptime(a,format='%Y-%m-%dT%H:%M')-strptime(b,format='%Y-%m-%dT%H:%M') > > > > The result shows > > > > Time difference of -41.06944 days. > > > > However when these operations performed in a dataframe, the time > difference > > is in the unit of seconds. Why is that? What I really want is in hours. I > > know I can convert the results manually to hours but just wonder if there > > is way to control the unit. Thanks. > > ?difftime > ?`-.POSIXt` > > Should provide exactly what is requested (and is linked from the > ?DateTimeClasses help page.) > > -- > David Winsemius > Alameda, CA, USA > > [[alternative HTML version deleted]] ______________________________________________ R-help@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.