11.2 条件执行

has_name <- function(x) {
  nms <- names(x)
  if (is.null(nms)) {
    rep(FALSE, length(x))
  } else {
    !is.na(nms) & nms != ""
  }
}

11.2.1 多条件执行

if (this) {
  # do that
} else if (that) {
  # do something else
} else {
  # 
}

当需要很多if时可考虑用switch()功能

function(x, y, op) {
   switch(op,
     plus = x + y,
     minus = x - y,
     times = x * y,
     divide = x / y,
     stop("Unknown op!")
   )
 }