[R] Reading data from the console
Jerome Asselin
jerome at hivnet.ubc.ca
Fri Jul 11 19:45:33 CEST 2003
Put your code in a function. See below.
Cheers,
Jerome
mat <- function()
{
cat('Number of populations:', '\n')
m<-scan("",n=1, quiet=TRUE)
cat('Number of categories:', '\n')
k<-scan("",n=1, quiet=TRUE)
N<-matrix(0,m,k)
for(i in 1:m) for(j in 1:k) {
N[i,j]<-scan("",n=1, quiet=TRUE)
}
N
}
> mat()
Number of populations:
1: 2
Number of categories:
1: 2
1: 1
1: 2
1: 3
1: 4
[,1] [,2]
[1,] 1 2
[2,] 3 4
On July 11, 2003 01:29 pm, Doru Cojoc wrote:
> I want to be able to write a program in R that does the following:
> - it allows the user to enter the dimensions of the matrix from the
> console
> - it allows the user then to enter each element of the matrix from the
> console.
>
> I am looking for an equivalent for the C++ command read, or read.ln.
> read.table would not work, since the data is not in a table and,
> furthermore, since the data does not exist prior to the execution of the
> program; the user has to be able to introduce the data from the console.
> scan would not work either. For example, in the following bit of code
>
> cat('Number of populations:', '\n')
> m<-scan("",n=1, quiet=TRUE)
> cat('Number of categories:', '\n')
> k<-scan("",n=1, quiet=TRUE)
> N<-matrix(0,m,k)
> for(i in 1:m) for(j in 1:k) {
> N[i,j]<-scan("",n=1, quiet=TRUE)
> }
>
> scan will take m to be the next command rather than wait for me to
> introduce the number I want for m.
>
> Any ideas? Thanks a lot!
>
> Doru Cojoc
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
More information about the R-help
mailing list