[R] Re ading user input (Readline)

skyjo srjohnson85 at hotmail.com
Wed Oct 28 23:25:29 CET 2009


Hello. I am trying to write an interactive function that asks the user for a
vector of observations. Unfortunately, if a user inputs a vector, R treats
the vector name as a string instead of a variable. Here is an example:


vector.input<-function(){
   k<-as.integer(readline("Input number of vectors: "))
   obs<-as.integer(readline("Input number of observations per vector
(assumed equal): "))
   matrix<-matrix((rep(0,k*obs)),ncol=k)
   for(f in 1:k){
     matrix[,f]<-as.vector(readline("Input vector of observations: "))
   }
   matrix
}

v1<-c(1:5)
v2<-c(6:10)

> vector.input()
Input number of vectors: 2
Input number of observations per vector (assumed equal): 5
Input vector of observations: v1
Input vector of observations: v2
     [,1] [,2]
[1,] "v1" "v2"
[2,] "v1" "v2"
[3,] "v1" "v2"
[4,] "v1" "v2"
[5,] "v1" "v2"

This resulting matrix is obviously not what I wanted. How can I get the
final matrix to look like:

> matrix
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10

?

Thanks in advance,
Skyler

-- 
View this message in context: http://www.nabble.com/Reading-user-input-%28Readline%29-tp26101738p26101738.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list