dimanche 28 octobre 2018

Solving "the condition has length > 1 and only the first element will be used" while executing function

I have defined this function with three parameters

` sleep.Min<- function(date,sleep,Wakeup){
 sl<-as.POSIXct(paste(date,"00:00:00 EST")) + (3600*sleep)
 wu<-as.POSIXct(paste(date,"00:00:00 EST")) + (3600*Wakeup)
  if (wu < sl) 
   {wu<-as.POSIXct(paste(date+1,"00:00:00 EST")) + (3600*Wakeup)}
   tim<-as.numeric(difftime(wu, sl, units = "mins"))
   return(tim)
   }`

The I execute it in the form:

sleeptime<-data.frame(sleep.Min(df$date,df$sleep,df$wakeup))

But then i get the error and as I can see in the output it do not execute the conditional "wu" despite it should, it always does in the same way.

Warning message:
In if (wu < sl) { :
the condition has length > 1 and only the first element will be used

I have check to do the funtion in other 100 ways but I can´t apply the conditional properly.

Aucun commentaire:

Enregistrer un commentaire