[R] need help to manipulate function and time interval

Uwe Ligges ligges at statistik.uni-dortmund.de
Mon Aug 13 10:39:37 CEST 2007



KOITA Lassana - STAC/ACE wrote:
> Hi,
> data are extracted from MS Access with the format: ("%d/%m%Y %H%M%S"); ex: 
> 16/09/2006 03:38:37


1. the example you gave in your first message is still not reproducible, 
you gave the following:

myfunc <- function(mytab, Time, Level)
{
vect <- rep(0, length(mytab))
for(i in 1:length(vect))
{
     for(j in 1:length(Time))
         if(time[j] is between 18:00:00 and 23:59:59)
             L[i] <- L[j]+5
         vect[i] <- 10^((L[i])/10
         if (time[j] is between 22:00:00 and 05:59:59)
             L[i] <- L[j]+10
         vect[i] <- 10^((L[i])/10
         else
             L[i] = L[j]
         vect[i] <- 10^((L[i])/10
      }
}


a) Please check the parentheses  (some closing ")" are missing, you also 
probably want to group some code "{}" that is executed in some of the 
"if" conditions rather than executing the (syntactically erroneous) line
           vect[i] <- 10^((L[i])/10
three times for each inner loop. Particularly, the "else" is 
syntactically incorrect with not preceding "if".

So my guess is you want something like


myfunc <- function(mytab, Time, Level)
{
vect <- rep(0, length(mytab))
for(i in 1:length(vect))
{
     for(j in 1:length(Time))
     {
         if(time[j] is between 18:00:00 and 23:59:59){
             L[i] <- L[j]+5
             vect[i] <- 10^(L[i]/10)
         }
         if (time[j] is between 22:00:00 and 05:59:59){
             L[i] <- L[j]+10
             vect[i] <- 10^(L[i]/10)
         } else {
             L[i] <- L[j]
             vect[i] <- 10^(L[i]/10)
         }
     }
}

But I am really not sure about that and why you subsitute L[i] so many 
times...!


b) What are mytab, Time and Level?


2. Which is the format do you have?
("%d/%m%Y %H%M%S")
*or*
16/09/2006 03:38:37
If the latter, you can use
dat <- strptime("16/09/2006 03:38:37", "%d/%m/%Y %H:%M:%S")
in order to get some POSIXlt object for later calculations.


3. What do you mean with
time[j] is between 22:00:00 and 05:59:59
Is the day of the date important or not for this comparison?

Uwe Ligges












> I still have error messneger
> 
> Thank you for your help. 
> 
> Lassana KOITA 
> Chargé d'Etudes de Sécurité Aéroportuaire et d'Analyse Statistique  / 
> Project Engineer Airport Safety Studies & Statistical analysis
> Service Technique de l'Aviation Civile (STAC) / Civil Aviation Technical 
> Department 
> Direction Générale de l'Aviation Civile (DGAC) / French Civil Aviation 
> Headquarters
> Tel: 01 49 56 80 60
> Fax: 01 49 56 82 14
> E-mail: Lassana.Koita at aviation-civile.gouv.fr
> http://www.stac.aviation-civile.gouv.fr/
> 
> 
> 
> Uwe Ligges <ligges at statistik.uni-dortmund.de> 
> 12/08/2007 15:24
> 
> A
> Matthew Walker <m.g.walker at massey.ac.nz>
> cc
> Henrique Dallazuanna <wwwhsd at gmail.com>, r-help at stat.math.ethz.ch, KOITA 
> Lassana - STAC/ACE <lassana.koita at aviation-civile.gouv.fr>
> Objet
> Re: [R] need help to manipulate function and time interval
> 
> 
> 
> 
> 
> 
> 
> 
> Matthew Walker wrote:
>> 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?
> 
> Well, the interesting part of the original question that everynody seems 
> to omit now was the second part:
> 
> if (time[j] is between 22:00:00 and 05:59:59)
> 
> hence the answer is still not sufficient (even if the syntax error has 
> been corrected) - and hence I asked for the format the time is 
> originally in.
> 
> Uwe Ligges
> 
> 
> 
> 
>> Cheers,
>>
>> Matthew
> 
> 
> 	[[alternative HTML version deleted]]
> 
> 
> 
> ------------------------------------------------------------------------
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.



More information about the R-help mailing list