2015-10-12 32 views
0

我想讀txt文件表如下: (當打開它看起來舉辦的txt文件,並確定)函數read.table

> 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源文件中獲取它。

+1

嘗試使用'sep =「\ t」'。文件中的分隔符是一個製表符,而不是逗號。 – Benjamin

回答

3

從您的示例代碼看來,在第一種情況下,您的文件確實不是用逗號分隔的,而是用製表符分隔的。省略sep = ','參數應導致正確讀取文件。