如何使用 grepl 验证用户输入的条件
How do I validate a user input for conditions using grepl
我正在努力坚持使用基础 R 作为个人挑战。不过,我并不打算使用 grepl,这只是我的第一个想法。我遇到了障碍。
这是我目前的情况:
armstrong <- function(x) {
if (grepl("^[-]{0,1}[0-9]{0,}.{0,1}[0-9]{1,}$", x) == FALSE) {
stop("Please try submitting a valid number")
}
else {
temp <- strsplit(as.character(x), split = "")
y <- sapply(temp, function(y)sum(as.numeric(y)^length(y)))
if (y == x) {
print(paste("The number you entered,", x ,", is an Armstrong number"))
}
else {
print(paste("The number you entered,", x ,", is not an Armstrong number"))
}
}
}
armstrong(readline(prompt = "Please enter a three digit positive number"))
程序检查用户是否输入了 Armstrong number。那部分有效。我坚持的是用户输入的错误处理。如果用户输入负数,或者如果他们输入数字之间的字符,例如 1a4,我无法让 grepl 输出 FALSE。如果 grepl 输出 FALSE 是很好的,因为用户输入的数字不是 3 位数字,但这没什么大不了的,因为该功能仍然有效。
如何更新正则表达式以帮助处理错误?
为什么不只是
if (! is.numeric(x) || x < 0) stop(…)
换句话说:使用正确的类型。如果输入应该是数字,则不接受字符串。
如果您需要接受字符串,将该字符串转换为数字并测试转换是否成功。您可以测试 NA
以检查它是否成功。
我还建议对函数进行一些其他更改:
armstrong <- function (x) {
num <- as.numeric(x)
if (is.na(num) || num < 0) stop("Please try submitting a valid number")
digits <- strsplit(x, "")[[1L]]
sum <- sum(as.numeric(digits) ^ length(digits))
msg <- paste0(
"The number you entered, ", x, ", is ",
if (num != sum) "not " else "",
"an Armstrong number"
)
message(msg)
}
我正在努力坚持使用基础 R 作为个人挑战。不过,我并不打算使用 grepl,这只是我的第一个想法。我遇到了障碍。
这是我目前的情况:
armstrong <- function(x) {
if (grepl("^[-]{0,1}[0-9]{0,}.{0,1}[0-9]{1,}$", x) == FALSE) {
stop("Please try submitting a valid number")
}
else {
temp <- strsplit(as.character(x), split = "")
y <- sapply(temp, function(y)sum(as.numeric(y)^length(y)))
if (y == x) {
print(paste("The number you entered,", x ,", is an Armstrong number"))
}
else {
print(paste("The number you entered,", x ,", is not an Armstrong number"))
}
}
}
armstrong(readline(prompt = "Please enter a three digit positive number"))
程序检查用户是否输入了 Armstrong number。那部分有效。我坚持的是用户输入的错误处理。如果用户输入负数,或者如果他们输入数字之间的字符,例如 1a4,我无法让 grepl 输出 FALSE。如果 grepl 输出 FALSE 是很好的,因为用户输入的数字不是 3 位数字,但这没什么大不了的,因为该功能仍然有效。
如何更新正则表达式以帮助处理错误?
为什么不只是
if (! is.numeric(x) || x < 0) stop(…)
换句话说:使用正确的类型。如果输入应该是数字,则不接受字符串。
如果您需要接受字符串,将该字符串转换为数字并测试转换是否成功。您可以测试 NA
以检查它是否成功。
我还建议对函数进行一些其他更改:
armstrong <- function (x) {
num <- as.numeric(x)
if (is.na(num) || num < 0) stop("Please try submitting a valid number")
digits <- strsplit(x, "")[[1L]]
sum <- sum(as.numeric(digits) ^ length(digits))
msg <- paste0(
"The number you entered, ", x, ", is ",
if (num != sum) "not " else "",
"an Armstrong number"
)
message(msg)
}