[R] newbie question: how to read a file into a matrix

Sundar Dorai-Raj sundar.dorai-raj at PDF.COM
Thu Aug 26 18:18:42 CEST 2004



Gaby Aguilera wrote:

> I am trying to read a file with 8 columns and about 200 rows into a
> matrix.  I tried something like:
> x<-matrix(data="data.txt", nrow=200, ncol=8) but this produces a
> matrix with "data.txt" in it.  Is there a way to read the data in?
> 
> Any pointers in the right direction would be appreciated.  Thank you!
> 

You should read "R Data Import/Export" (and the posting guide).

http://cran.r-project.org/doc/manuals/R-data.pdf

What you are looking for is ?read.table or ?scan. Also read ?matrix 
while you are at it.

x <- matrix(scan("data.txt"), nrow = 200, ncol = 8)

--sundar




More information about the R-help mailing list