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.
?get On 29/10/2009, at 11:25 AM, skyjo wrote:> > 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. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
Thanks! Rolf Turner-3 wrote:> > > ?get > > On 29/10/2009, at 11:25 AM, skyjo wrote: > >> >> 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. >> >> ______________________________________________ >> R-help at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting- >> guide.html >> and provide commented, minimal, self-contained, reproducible code. > > > ###################################################################### > Attention:\ This e-mail message is privileged and confid...{{dropped:9}} > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://www.nabble.com/Reading-user-input-%28Readline%29-tp26101738p26103352.html Sent from the R help mailing list archive at Nabble.com.