2016-07-13 24 views
2

我有一个文本文件读入R,但该文件似乎不是制表符分隔。文件的唯一结构是列总是在某个点完成(即列是右对齐的)。read.table与“右对齐”数据

因此,首先,这种类型的数据结构是否有名称?那么,如何在R中读取?

2.37  2.03       2.38 
    5,397  5,082       5,609 
    13.0  21.6   15.2   15.2 
    128.0  103.1   134.2   133.4 

只需使用函数read.table()不工作,缺少的值不会在正确的地方放...

# download data: 
tmp <- tempfile() 
f <- download.file("http://usda.mannlib.cornell.edu/usda/waob/wasde//1990s/1995/wasde-01-12-1995.txt", tmp) 
D <- file(tmp) 
data_enc <- readLines(D, warn=FALSE) 
close(D) 
dat <- sapply(strsplit(data_enc[232:236], ":"), function(x) x[2]) 
writeLines(dat, tmp) 

## try to read data: 
read.table(tmp, fill = TRUE, sep ="", header=FALSE) 

给出:

 V1 V2 V3 V4 
1 2.37 2.03 2.38 NA 
2 5,397 5,082 5,609 NA 
3 13.0 21.6 15.2 15.2 
+0

相关文章:http://stackoverflow.com/questions/24715894 – zx8754

回答

1

也许尝试使用read.fwf来读取固定宽度格式化数据的表格:

widths <- gregexpr("\\.\\d", readLines(tmp)[5])[[1]]+1L # line 5 looks complete 
widths <- c(widths[1], diff(widths)) # posis after the decimal points as widths 
read.fwf(tmp, widths = widths) 
#   V1   V2 V3    V4 
# 1  2.37  2.03 NA    2.38 
# 2 5,397  5,082 NA   5,609 
# 3  13.0  21.6 15.2    15.2 
# 4 128.0  103.1 134.2   133.4 
# 5 146.4  130.9 156.5   155.7 
+1

哦,真好!那么这个数据仍然是一个固定宽度的数据?我被欺骗了,认为固定的宽度不仅涉及同样的目标,而且涉及同一个起点!那么也许哈德利的包装阅读器会让它变得非常简单:read_fwf(tmp,fwf_empty(tmp)) – Matifou

+0

......是的,甚至更好。 :-) – lukeA