如何允许用户在R中的最小-最大范围内输入?

如何允许用户在R中的最小-最大范围内输入?,r,R,下午好 我的问题很简单 我想检索一些n个用户输入(长度为n的向量)。仅接受介于0和1之间的值 我知道如何使用scan功能检索值,但我不知道如何强制用户只在[min-max]间隔内输入值 谢谢你的帮助 代码: x <- scan(,n=3) x一种方法是使用while循环: stayInLoop <- TRUE N<-3 # number of elements in vector while(stayInLoop){ print("Please insert x")

下午好

我的问题很简单

我想检索一些n个用户输入(长度为n的向量)。仅接受介于0和1之间的值

我知道如何使用
scan
功能检索值,但我不知道如何强制用户只在[min-max]间隔内输入值

谢谢你的帮助

代码:

x <- scan(,n=3)

x一种方法是使用
while循环

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

stayInLoop正常!我认为有一个内置选项,但没有问题。谢谢你宝贵的帮助!