[英]How to allow user inputs in a min-max range in R?
Good afternoon.下午好。
My question is very simple.我的问题很简单。
I'm wanting to retrieve some n user inputs ( a vector of length n ).我想检索一些 n 用户输入(长度为 n 的向量)。 Only values between 0 and 1 are accepted.
只接受 0 到 1 之间的值。
I know how to retrieve values with scan
function but i don't know how to force users to enter only values in [min-max] interval.我知道如何通过
scan
function 检索值,但我不知道如何强制用户仅在 [min-max] 间隔中输入值。
Thank you for help !谢谢你的帮助 !
Code:代码:
x <- scan(,n=3)
One way can be using a while loop
:一种方法是使用
while loop
:
stayInLoop <- TRUE
N<-3 # number of elements in vector
while(stayInLoop){
print("Please insert x")
x <- scan(,n=N) #readLines(,n=N)
if (any(x<0) | any(x>1)) {
print("Re-enter the values, as valid values can be between 0 and 1")
x <- scan(,n=N)
}
stayInLoop<-any(x<0) & any(x>1)
}
[1] "Please insert x"
1: 1
2: 2
3: 3
Read 3 items
[1] "Re-enter the values, as valid values can be between 0 and 1"
1: 0.2
2: 0.2
3: 0.4
Read 3 items
> x
[1] 0.2 0.2 0.4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.