jeudi 4 juin 2020

R Unexpected token "else" in else if statement

I'm trying to create an if loop containing two else if in r, however I I get the following error: "unexpected token else" screenshot of the error in r

for these two else if:

else if(x=="container10ft") else if(x=="container20ft")

This is the code:

nearest_hub <- name_closest(request_info$location[1])
second_nearest <- name_second(request_info$location[1])
third_nearest <- name_third(request_info$location[1])
fourth_nearest <- name_fourth(request_info$location[1])
x= "container10ft"
n= 1

con_name= request_info$location[1]
 if(x == "trailer"){
    if( (hubs[nearest_hub,5]+ n) <= 6 && (hubs[nearest_hub,4]+ n) <= 2 && (hubs[nearest_hub,3]+ n) <= 1){
    hubs[nearest_hub,5] = hubs[nearest_hub,5] + n 
    #up to now it decreases the no. of space available but I do not know how to syncronize the duration of request and the time of booking
    shipment_cost = 0
    } else if( (hubs[second_nearest,5]+ n) <= 6 && (hubs[second_nearest,4]+ n) <= 2 && (hubs[second_nearest,3]+ n) <= 1) {
    hubs[second_nearest,5] = hubs[second_nearest,5]+ n
    shipment_cost <- shipment_nearest_2(con_name)*cost_trailer_shipment*n
      } else if( (hubs[third_nearest,5]+ n) <= 6 && (hubs[third_nearest,4]+ n) <= 2 && (hubs[third_nearest,3]+ n) <= 1){
    hubs[third_nearest,5] = hubs[third_nearest,5]+ n
    shipment_cost <- shipment_nearest_3(con_name)*cost_trailer_shipment*n
      } else if( (hubs[fourth_nearest,5]+ n) <= 6 && (hubs[fourth_nearest,4]+ n) <= 2 && (hubs[fourth_nearest,3]+ n) <= 1){
     hubs[fourth_nearest,5] = hubs[fourth_nearest,5]+ n
    shipment_cost <- shipment_nearest_3(con_name)*cost_trailer_shipment*n
      }}
  else if(x=="container10ft"){
    if((hubs[nearest_hub,5] + n) <= 6 && (hubs[nearest_hub,4] + n) <= 2 && (hubs[nearest_hub,3] + n) <= 1){
    hubs[nearest_hub,4] = hubs[nearest_hub,4] + n
    #up to now it decreases the no. of space available but I do not know how to syncronize the duration of request and the time of booking
    shipment_cost <-shipment_nearest(con_name)* cost_10ft_shipment
    } else if((hubs[second_nearest,5] + n) <= 6 && (hubs[second_nearest,4] + n) <= 2 && (hubs[second_nearest,3] + n) <= 1) {
    hubs[second_nearest,4] = hubs[second_nearest,4] + n
    shipment_cost <- shipment_nearest_2(con_name)*cost_10ft_shipment
      } else if((hubs[third_nearest,5] + n) <= 6 && (hubs[third_nearest,4]+ n) <= 2 && (hubs[third_nearest,3] + n) <= 1){
    hubs[third_nearest,4] = hubs[third_nearest,4] + n
    shipment_cost <- shipment_nearest_3(con_name)*cost_10ft_shipment
      } else if((hubs[fourth_nearest,5] + n) <= 6 && (hubs[fourth_nearest,4] + n) <= 2 && (hubs[fourth_nearest,3] + n) <= 1){
    hubs[fourth_nearest,4] = hubs[fourth_nearest,4] + n
    shipment_cost <- shipment_nearest_3(con_name)*cost_10ft_shipment
      }
  }
  else if(x=="container20ft"){
    if((hubs[nearest_hub,5] + n) <= 6 && (hubs[nearest_hub,4] + n) <= 2 && (hubs[nearest_hub,3] + n) <= 1){
    hubs[nearest_hub,3] = hubs[nearest_hub,3]+ n
    #up to now it decreases the no. of space available but I do not know how to syncronize the duration of request and the time of booking
    shipment_cost <- shipment_nearest(con_name)*cost_20ft_shipment
    } else if((hubs[second_nearest,5] + n) <= 6 && (hubs[second_nearest,4] + n) <= 2 && (hubs[second_nearest,3] + n) <= 1) {
    hubs[second_nearest,3] = hubs[second_nearest,3] + n
    shipment_cost <- shipment_nearest_2(con_name)*cost_20ft_shipment
      } else if((hubs[third_nearest,5] + n) <= 6 && (hubs[third_nearest,4] + n) <= 2 && (hubs[third_nearest,3] + n) <= 1){
    hubs[third_nearest,3] = hubs[third_nearest,3] + n
    shipment_cost <- shipment_nearest_3(con_name)*cost_20ft_shipment
      } else if((hubs[fourth_nearest,5] + n) <= 6 && (hubs[fourth_nearest,4]+ n) <= 2 && (hubs[fourth_nearest,3] + n) <= 1){
    hubs[fourth_nearest,3] = hubs[fourth_nearest,3] + n
    shipment_cost <- shipment_nearest_3(con_name)* cost_20ft_shipment }} 

` Therefore, whenever I try tu run the code, R just runs the first if statement and then it stops. It seems r does not recognize that x could have three different options ( trailer, container10ft and container20ft). Thank you in advance

Aucun commentaire:

Enregistrer un commentaire