Hi guys, I have a large text file with a bunch of Time in "HH:MM" format, what would be the best way to process it into a Time Object so that I can use comparisons like (1:00<"1:15") or (13:00>"2:00") to both return true. Right now if I do a comparison like (3:00 < "1:59") I get a true, but if I do (3:00 < "2:00") I get false, which is an obvious error. Thanks, Allan -- View this message in context: http://r.789695.n4.nabble.com/Best-way-to-Convert-String-to-Time-for-comparison-tp2313386p2313386.html Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2010-Aug-04 12:47 UTC
[R] Best way to Convert String to Time for comparison?
On Wed, Aug 4, 2010 at 8:43 AM, allany <allany at cmu.edu> wrote:> > Hi guys, > > I have a large text file with a bunch of Time in "HH:MM" format, what would > be the best way to process it into a Time Object so that I can use > comparisons like (1:00<"1:15") or (13:00>"2:00") to both return true. > > Right now if I do a comparison like (3:00 < "1:59") I get a true, but if I > do (3:00 < "2:00") I get false, which is an obvious error. > >Try this: library(chron) t1 <- times(paste("13:00", "00", sep = ":")) t2 <- times(paste("2:00", "00", sep = ":")) t1 > t2
Another way is
t1<- c("3:00","1:59","3:00","2:00")
t2 <- strptime(t1, format="%H:%M")
t2[-4]>t2[-1]
Nikhil Kaza
Asst. Professor,
City and Regional Planning
University of North Carolina
nikhil.list at gmail.com
On Aug 4, 2010, at 8:47 AM, Gabor Grothendieck wrote:
> On Wed, Aug 4, 2010 at 8:43 AM, allany <allany at cmu.edu> wrote:
>>
>> Hi guys,
>>
>> I have a large text file with a bunch of Time in "HH:MM"
format,
>> what would
>> be the best way to process it into a Time Object so that I can use
>> comparisons like (1:00<"1:15") or
(13:00>"2:00") to both return true.
>>
>> Right now if I do a comparison like (3:00 < "1:59") I get
a true,
>> but if I
>> do (3:00 < "2:00") I get false, which is an obvious error.
>>
>>
>
> Try this:
>
> library(chron)
> t1 <- times(paste("13:00", "00", sep =
":"))
> t2 <- times(paste("2:00", "00", sep =
":"))
> t1 > t2
>
> ______________________________________________
> 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.