0

I know that readline() is used to get user input. But how to use it to get a range of values?

Eg: I need to get 10 random numbers from user and want to do some operations on them. Please help!

PS: I am totally new to R.

Harish
  • 285
  • 7
  • 20
  • This is not a duplicate question. I want to input multiple values in a same variable. The other question was different. – Harish Jul 10 '17 at 11:54

1 Answers1

0

scan() lets you enter multiple inputs, separated by a space or a newline and termined by two newlines.

Assigning the result x <- scan() yields a vector in case of multiple inputs.

This does not ensure that you get exactly ten inputs though.

If you want exactly ten inputs, you could prompt ten times:

x <- numeric(10)
for(i in 1:10){
  x[i] <- readLines()
}
JAD
  • 2,035
  • 4
  • 21
  • 35
  • Thanks a lot. Scan was the function i was looking for. I have doubt. Why do you use <- for variable assignment when = works fine? Wish to understand. Isn't "=" always easier? – Harish Jul 10 '17 at 12:00
  • 1
    Both work, but `=` is ambiguous. It can mean different things in different contexts, while `<-` can only mean one thing. So when reading it is directly obvious what is meant. See https://google.github.io/styleguide/Rguide.xml – JAD Jul 10 '17 at 13:02