11.2 条件执行
<- function(x) {
has_name <- names(x)
nms 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!")
) }