2013-02-18 31 views
0

我想用讀取數據文件:作爲分隔符如何使用函數read.table文件與分隔符 t

ds <- read.table(file="/data/ken/tmp/tt", header=F, 
        sep="\t", quote="\"", dec=".", 
        fill=T, comment.char="", 
        stringsAsFactors=F, 
        colClass=rep("character", 6)) 

文件tt看起來跟隨,與\t

20130129074502\thttp://xxx.com.cn/notebook/asus/526600_detail.html\t\t5025\t526600\t255dkmi 

但它不起作用:

caution: 
In read.table(file = fcon, header = F, sep = "\t", quote = "\"", : 
    cols = 1 != length(data) = 6 
+0

是否'\ t'代表2逐行讀取,並分字節字符串「\ t」或「tab」? – wush978 2013-02-18 06:25:08

+0

爲什麼不使用'read.delim'來設置製表符分隔文件? – 2013-02-18 06:50:37

+0

對於傻笑,試試'sep =「\\ t」'。 – 2013-02-18 08:19:15

回答

0

我認爲你在複雜的事情,試試這個:

read.table(text=tt) 
      V1             V2 V3  V4  V5 
1 2.013013e+13 http://xxx.com.cn/notebook/asus/526600_detail.html 5025 526600 255dkmi 

其中TT是:

tt ='20130129074502\thttp://xxx.com.cn/notebook/asus/526600_detail.html\t\t5025\t526600\t255dkmi' 

編輯

可以使用strsplit

sapply(readLines(file.name, n=-1), 
       function(x) strsplit(gsub('[\t|\t\\]','@',x),'@')) 
+0

,因爲它沒有創造性,導致它在原始數據中有6列(用5'\ t'),但是你只能得到5列 – catchyoulater 2013-02-18 08:21:04

+0

這個列的限制在哪裏?你可以告訴我..也許我需要分割列V2 ... – agstudy 2013-02-18 08:33:41

+0

兩個'\ t'之間有一個空字符串,這就像原始數據中的「... detail.html \ t \ t5025」。看起來函數read.table忽略sep如果'\ t'帶有一個空字符串。 – catchyoulater 2013-02-18 09:43:59

相關問題