首页 > 代码库 > R:从文本文件读取矩阵

R:从文本文件读取矩阵

在~下有一文本文件matrix.dat内容如下:
99 102 3
12 43 213.0
12 12 2
23 21 211



方式1:
> A <- matrix(scan("~/matrix.dat", n = 3*4), 3, 4, byrow = TRUE)
Read 12 items
> A
     [,1] [,2] [,3] [,4]
[1,]   99  102    3   12
[2,]   43  213   12   12
[3,]    2   23   21  211



方式2:
> A <- as.matrix(read.table("matrix.dat"))
> A
     V1  V2  V3
[1,] 99 102   3
[2,] 12  43 213
[3,] 12  12   2
[4,] 23  21 211



参考:
https://stat.ethz.ch/pipermail/r-help/2006-June/108227.html

R:从文本文件读取矩阵