R else if 语句中的意外标记 "else"

R Unexpected token "else" in else if statement

我试图在 r 中创建一个包含两个 else if 的 if 循环,但是我收到以下错误: "unexpected token else" screenshot of the error in r

对于这两个,如果:

否则如果(x=="container10ft") 否则如果(x=="container20ft")

这是代码:

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 }} 

` 因此,每当我尝试 tu 运行 代码时,R 只是 运行 第一个 if 语句然后它停止。 r 似乎没有意识到 x 可以有三个不同的选项(trailer、container10ft 和 container20ft)。提前谢谢你

如果您删除换行符并将 else 放在与结束 } 相同的行中,它在我的系统上有效。

简单的例子。这有效:

x <- 1
if (x == 0) {
  print(1)
} else print(2)

这不是:

x <- 1
if (x == 0) {
  print(1)
} 
else print(2)