[R] Converting a .txt file into a matrix
Steve Lianoglou
mailinglist.honeypot at gmail.com
Tue Apr 13 20:12:43 CEST 2010
Hi,
On Tue, Apr 13, 2010 at 12:37 PM, cobbler_squad <la.foma at gmail.com> wrote:
>
> I need to convert foo.txt file into as.matrix
> .txt file is a single column of numbers
> (i.e.
> -0.303904
> -0.889965
> -0.0270313
> -0.387125
> 0.189837
> -0.14858
> -0.651178
> -0.162632
> 0.449309
> )
>
> and I need to find out the correct syntax to read in this table as.matrix
>
> I tried as.matrix(read.table(foo.txt)), but unfortunately this just spits
> the table back out..
>
> Any of your pointers would be welcome..
Assuming `as.matrix` would have worked, how would it know the number
of rows/columns you need for your matrix?
You can read in your numbers with the `readLines` command, convert
them from characters using `as.numeric`, then build a matrix with
`matrix`:
R> dat <- as.numeric(readLines('fool.txt'))
R> dat.matrix <- matrix(dat, nrow=??)
Note that `dat.matrix` will be filled in column by column, not row by
row. If you want the numbers to be filled in row-by-row, then set
`byrow=TRUE` in your call to `matrix`.
--
Steve Lianoglou
Graduate Student: Computational Systems Biology
| Memorial Sloan-Kettering Cancer Center
| Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact
More information about the R-help
mailing list