以下函數採用長度爲1或2的字符向量x,並檢查它是否包含變量a和b中的字符串 - 注意x不應該包含a和b在同一個元素中。 然後它返回a或b或兩者。 有沒有更乾淨的方法來做到這一點?R中的模式匹配有兩種模式和一個列表輸入
match_vec <- function(x, a = "alpha", b = "beta") {
stopifnot(is.character(x), is.character(a), is.character(b))
stopifnot(length(a) == 1, length(b) == 1)
stopifnot(length(x) %in% c(1, 2))
ai <- grep(a, x)
bi <- grep(b, x)
if (length(x) == 1) {
if (length(ai) == 1 && length(bi) == 0) {
return(a)
} else if (length(ai) == 0 && length(bi) == 1) {
return(b)
} else {
stop(paste("Could not find", a, "xor", b, "in:", x))
}
}
if (length(x) == 2) {
if (length(ai) == 1 && length(bi) == 1) {
stopifnot(ai != bi)
return(c(a, b))
} else {
stop(paste("Could not find both", a, "and", b, "in:", x, collapse = "\n"))
}
}
}
match_vec(c("alpha", "beta"))
#[1] "alpha" "beta"
match_vec(c("beta", "alpha"))
#[1] "alpha" "beta"
match_vec("alpha")
#[1] "alpha"
match_vec("betaaaa")
#[1] "beta"
match_vec("bettaaaa")
#Error in match_vec("bettaaaa") :
# Could not find alpha xor beta in: bettaaaa
match_vec("alphaandbeta")
#Error in match_vec("alphaandbeta") :
# Could not find alpha xor beta in: alphaandbeta
match_vec(c("this has thealpha", "beta in here"))
#[1] "alpha" "beta"
你可以改變第二'if'到'else_if'。提高性能一點。 –