read.table 来自 txt 文件

read.table from txt file

我尝试从 txt 文件中读取 table,如下所示: (当打开 txt 文件时,它看起来井井有条)

> dat1<-read.table (file.choose(), header = TRUE,sep = ",")
> head (dat1)
  b.e.LBE.LB.AC.FM.UC.ASTV.MSTV.ALTV.MLTV.DL.DS.DP.DR.NSP.testing.ds.1..ds.2..ds.3..ds.4.
1                               1\t240\t357\t120\t120\t0\t0\t0\t73\t0.5\t43\t2.4\t0\t0\t0\t0\t2\t0\t1\t0\t0\t0
2                                 2\t5\t632\t132\t132\t4\t0\t4\t17\t2.1\t0\t10.4\t2\t0\t0\t0\t1\t0\t0\t1\t0\t0
3                               3\t177\t779\t133\t133\t2\t0\t5\t16\t2.1\t0\t13.4\t2\t0\t0\t0\t1\t0\t1\t0\t0\t0
4                                4\t411\t1192\t134\t134\t2\t0\t6\t16\t2.4\t0\t23\t2\t0\t0\t0\t1\t0\t0\t0\t0\t1
5                              5\t533\t1147\t132\t132\t4\t0\t5\t16\t2.4\t0\t19.9\t0\t0\t0\t0\t1\t0\t0\t1\t0\t0
6                                   6\t0\t953\t134\t134\t1\t0\t10\t26\t5.9\t0\t0\t9\t0\t2\t0\t3\t0\t0\t1\t0\t0

当我将其转换为 CSV 文件并使用时

 > dat2<-read.table (file.choose(), header = TRUE,sep = ",")
I get the following required result:
  X   b    e LBE  LB AC FM UC ASTV MSTV ALTV MLTV DL DS DP DR NSP testing ds.1. ds.2. ds.3. ds.4.
1 1 240  357 120 120  0  0  0   73  0.5   43  2.4  0  0  0  0   2       0     1     0     0     0
2 2   5  632 132 132  4  0  4   17  2.1    0 10.4  2  0  0  0   1       0     0     1     0     0
3 3 177  779 133 133  2  0  5   16  2.1    0 13.4  2  0  0  0   1       0     1     0     0     0
4 4 411 1192 134 134  2  0  6   16  2.4    0 23.0  2  0  0  0   1       0     0     0     0     1
5 5 533 1147 132 132  4  0  5   16  2.4    0 19.9  0  0  0  0   1       0     0     1     0     0
6 6   0  953 134 134  1  0 10   26  5.9    0  0.0  9  0  2  0   3       0     0     1     0     0

如何在不转换为 CSV 的情况下获得上述要求的结果。我想直接从txt源文件中获取。

从你的示例代码来看,在第一种情况下,你的文件确实不是逗号分隔的,而是制表符分隔的。省略 sep = ',' 参数应该可以正确读取文件。