[R] Reading Data

Marc Schwartz MSchwartz at medanalytics.com
Fri Mar 19 18:28:24 CET 2004


On Fri, 2004-03-19 at 10:47, Kissell, Robert [EQRE] wrote:
> Hi,
> 
> Quick question on reading data.
> 
> I am running simulations but want to allow the user the option to
> define the number of simulations. How can I have R read-in user data
> entered from the keyboard? Is there a difference for reading in
> numeric and character data?
> 
> For example, I am trying to write the following in R:
> 
> Enter Number of Iterations?
> <<<the user then enters a number say Y >>>
> 
> X <- Y       # i then want to read that number into X
> 
> 
> Thanks.


See ?readline, which returns a character vector by default. Thus you
would need to convert the returned value to numeric:

#This returns "5" as a character
> n <- readline("Enter Number of Iterations? ")
Enter Number of Iterations? 5
> n
[1] "5"

# This returns 5 as a number
> n <- as.numeric(readline("Enter Number of Iterations? "))
Enter Number of Iterations? 5
> n
[1] 5


HTH,

Marc Schwartz




More information about the R-help mailing list