重复的else if语句的快捷方式

mob*_*upu 2 if-statement r

如果条件如下,我必须写重复的其他内容:

  if (d_hand[1]==1){
    state=score(p_hand)-1
  } else if (d_hand[1]==2){
    state=19+score(p_hand)
  } else if (d_hand[1]==3){
    state=39+score(p_hand)
  } else if (d_hand[1]==4){
    state=59+score(p_hand)
  } else if (d_hand[1]==5){
    state=79+score(p_hand)
  } else if (d_hand[1]==6){
    state=99+score(p_hand)
  }
Run Code Online (Sandbox Code Playgroud)

你知道它是否可以更快/更快地写出来吗?我想过做一个if循环,但效率会降低,因为必须检查每个语句.

Mar*_*zer 6

完全没有ifelse:

state <- score(p_hand) + 20 * d_hand[1] - 21
Run Code Online (Sandbox Code Playgroud)