[R] need help to manipulate function and time interval

Matthew Walker m.g.walker at massey.ac.nz
Sun Aug 12 02:29:50 CEST 2007


Uwe Ligges wrote:
> Henrique Dallazuanna wrote:
>   
>> Hi,
>>
>> Try whit:
>>
>> if(time[j] >= "18:00:00" & < "23:59:59")
>>     
>
> This code is obviously wrong and does not help for the next few lines in 
> the questioner's message, please do not post unsensible stuff.
>
> Uwe Ligges
>
>   

Actually, I would have said that was quite an interesting solution.  If 
the time is guaranteed to be in this 8 character format, then that 
idea's quite easily implemented:

# Returns true if time is between 18:00:00 and 23:59:59
check_time <- function(time_string) {
  if (nchar(time_string)!=8) stop ("Incorrect format")
  time_string >= "18:00:00" & time_string <= "23:59:59"
}


 > check_time("19:59:00")
[1] TRUE
 > check_time("16:59:00")
[1] FALSE
 >
 > check_time("18:00:00")
[1] TRUE
 > check_time("23:59:59")
[1] TRUE
 > check_time("24:00:00")
[1] FALSE
 >
 > check_time("18:05")
Error in check_time("18:05") : Incorrect format


Perhaps there is an issue if the locale does not sort character-based 
numbers in the same way as ASCII?  But Otherwise, I can't see why this 
solution wouldn't do the job.

A more robust solution solution would parse the strings (?strptime) and 
then check their days/hours/mins/seconds (?DateTimeClasses).  But 
perhaps the above is sufficient?

Cheers,

Matthew



More information about the R-help mailing list