How to solve Error in while (time <= time_end) { : missing value where TRUE/FALSE needed?

Viewed 8

I thought I accounted for the NULLs in the logic statement, but it's still throwing up an error. I'm not sure why. There are NULLs in the data that I cannot remove. Visual is a dataframe that contains a time value as an integer (ie 8:00 == 800) as well as some other character data. final_1 and final_2 are just empty versions of the Visual dataframe. I also need to write some code for the time logic, so that the hour jumps by 100 when the value reaches 60.

 for (i in 1:nrow(Visual)){
  time <- Visual[i,]$Start_Time
  time_end <- Visual[i,]$End_Time
  if(is.null(time)){
    time <- min_time
    if(is.null(time_end)){
      while(time <= max_time){
        final_2[i,]$Time <- time
        final_2[i,]$Appointment_AIM <- Visual[i,]$Appointment
        final_2[i,]$AIM_Abbreviation <- Visual[i,]$Abbreviation
        final_2[i,]$Standard_Duration <- Visual[i,]$Standard_Duration
        final_2[i,]$Booking_Factor <- Visual[i,]$Booking_Factor
        final_2[i,]$Appointment_Categories_ACM <- Visual[i,]$Appointment_Categories
        final_2[i,]$ACM_Abbreviation <- Visual[i,]$ACM_Abbreviation
        final_2[i,]$Color_Code <- Visual[i,]$Color_Code
        final_1 <- rbind(final_1, final_2)
        time <- time + Visual[i,]$Standard_Duration
      }
    }else{
      while(time <= time_end){
        final_2[i,]$Time <- time
        final_2[i,]$Appointment_AIM <- Visual[i,]$Appointment
        final_2[i,]$AIM_Abbreviation <- Visual[i,]$Abbreviation
        final_2[i,]$Standard_Duration <- Visual[i,]$Standard_Duration
        final_2[i,]$Booking_Factor <- Visual[i,]$Booking_Factor
        final_2[i,]$Appointment_Categories_ACM <- Visual[i,]$Appointment_Categories
        final_2[i,]$ACM_Abbreviation <- Visual[i,]$Abbreviation
        final_2[i,]$Color_Code <- Visual[i,]$Color_Code
        final_1 <- rbind(final_1, final_2)
        time <- time + Visual[i,]$Standard_Duration
      }
    }
  }else{
    if(is.null(time_end)){
      while(time <= max_time){
        final_2[i,]$Time <- time
        final_2[i,]$Appointment_AIM <- Visual[i,]$Appointment
        final_2[i,]$AIM_Abbreviation <- Visual[i,]$Abbreviation
        final_2[i,]$Standard_Duration <- Visual[i,]$Standard_Duration
        final_2[i,]$Booking_Factor <- Visual[i,]$Booking_Factor
        final_2[i,]$Appointment_Categories_ACM <- Visual[i,]$Appointment_Categories
        final_2[i,]$ACM_Abbreviation <- Visual[i,]$Abbreviation
        final_2[i,]$Color_Code <- Visual[i,]$Color_Code
        final_1 <- rbind(final_1, final_2)
        time <- time + Visual[i,]$Standard_Duration
      }
    }else{
      while(time <= time_end){
        final_2[i,]$Time <- time
        final_2[i,]$Appointment_AIM <- Visual[i,]$Appointment
        final_2[i,]$AIM_Abbreviation <- Visual[i,]$Abbreviation
        final_2[i,]$Standard_Duration <- Visual[i,]$Standard_Duration
        final_2[i,]$Booking_Factor <- Visual[i,]$Booking_Factor
        final_2[i,]$Appointment_Categories_ACM <- Visual[i,]$Appointment_Categories
        final_2[i,]$ACM_Abbreviation <- Visual[i,]$Abbreviation
        final_2[i,]$Color_Code <- Visual[i,]$Color_Code
        final_1 <- rbind(final_1, final_2)
        time <- time + Visual[i,]$Standard_Duration
      }
    }
  }
}
1 Answers

Be mindful about the difference between NAs and NULL values. Se the output of the following expressions:

is.null(NA)
[1] FALSE


while(NA) {TRUE}
"Error in while (NA) { : missing value where TRUE/FALSE needed"

If we are looking for conditional transformations that require nested if/else or ifelses, we may be better off using dplyr::case_when.

Related