简单条件

控制流 / 条件语句 / switch/case 语句

inch_size <- sample(c(14, 16, 19, 24, 32), 1)

# The last unnamed argument is the default
verdict <- switch(
  as.character(inch_size),
  "14" = "too small",
  "16" = "good for the past decade",
  "19" = "for office work",
  "24" = "great choice",
  "unknown size")

# Trap: no match and no default give NULL
no_default <- switch("40", "14" = "too small")

cat("inch_size =", inch_size, "\n")
cat("verdict =", verdict, "\n")
cat("is.null(no_default) =",
    is.null(no_default), "\n")
# is.null(no_default) = TRUE