Duncan
Duncan

Reputation: 1034

Reading multiple matrices from the same file in R

I know you can use read.table to read one matrix from a file, but I would like to read two matrices of the same size (m by n) from one file in R and put them in two separate R variables.

For example, this file contains two 3 by 2 matrices:

6 3
2 5
5 4
4 3
6 3
3 4

Upvotes: 0

Views: 506

Answers (1)

Jarrod Urban
Jarrod Urban

Reputation: 134

Here is my shot at it.

 split(read.table("data.txt"), gl(2, 3, labels=c("x1", "x2")))

It should be easy to generalize this and wrap it up into a function.

I hope this helps.

Upvotes: 1

Related Questions