2017-03-02 6 views
2

で読んでいます。これは非常に基本的な質問ですが、私はRに新しいので、この質問があります。キーボードの入力をR

キーボードから数字を入力する方法。 ユーザーがキーボードから入力したい番号の数を入力し、それに基づいて数字を入力する機能を提供します。

例:

How many numbers you want to enter? 
> 10 
Enter numbers: 
> 5 10 15 20 25 30 35 40 45 50 
+1

hello check '?readline' –

+0

2番目の@ s.brunelのコメントは、[readlineについてのドキュメント](https://stat.ethz.ch/R-manual/R-devel/library/base)を見てください。 /html/readline.html) –

答えて

0
while(T) { 
    num <- readline("How many number do you want to enter? > ") 
    num <- as.numeric(num) 
    if (!is.na(num)) { 
     num2 <- readline(paste0("Enter ",num, " numbers > ")) 
     print(num2) 
     break 
    } 
    } 
+0

ありがとう@ raistlin ..しかし、それは数字の検証を提供していません。ユーザーが最初の質問のための5として入力を提供しますが、ユーザーが上記のコードまたは5未満の数字の5つ以上の数字を入力することができます – Sriharsha

+0

おそらく問題は期待した成果についてはっきりしていない –

0

私はそれが整数

readnumber <- function() 
{ 
    n <- readline(prompt="How many numbers do you want to enter: ") 
    n <- as.integer(n) 
    if (is.na(n)){ 
     n <- readnumber() 
    } 
    Numbers<-c() 
    for (i in 1:n){ 
     num <- readline(prompt="Enter an integer: ") 
     Numbers[i]<-as.numeric(num) 
    } 
    return(Numbers)  
} 
print(readnumber()) 
を入力する機能を提供して数え、彼らが入力したいどのように多くの数のユーザーを尋ね、それに基づいてする関数を作成しました
関連する問題