简体   繁体   English

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

[英]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.

相关问题 R将增量值指定为最小-最大日期范围,按 - R give incremental value to range min-max Date, group by 根据R?中的计数最小-最大范围,在直方图的计数(y轴)上添加间隔。 - Adding breaks to count (y axis) of a histogram according to the count min-max range in R? 为min-max范围创建geom_ribbon - Create geom_ribbon for min-max range 对r中的数据集中的所有变量进行排名和最小-最大范数 - Rank and min-max norm all variables in a dataset in r R中的最小-最大归一化,根据另一列设置最小和最大组 - Min-max normalization in R, setting groups of min and max based on another column 使用Python或R通过最小-最大和标准偏差方法对仅某些列进行归一化 - Normalization by min-max & stand deviation method to only certain columns using Python or R preProc = c(“center”, “scale”) 在插入符号的 package (R) 和 min-max 归一化中的含义 - preProc = c(“center”, “scale”) meaning in caret's package (R) and min-max normalization 如何在表中为(Range)创建一行以将(Min:Max)一起包含在R中? - How to create a row in a table for (Range) to include (Min:Max) together in R? 如何在R Shiny中允许用户动态输入文件数量? - How to allow for dynamic amount of file inputs from user in R shiny? 如何允许用户使用R的多个输入? - How to allow multiple inputs from user using R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM