我有一个文本文件要在R中读取,但该文件似乎不是以制表符分隔的。文件的唯一结构是列总是在某个点结束(即列是右对齐的)。
那么,首先,这种类型的数据结构有名称吗?那么,如何在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.2https://stackoverflow.com/questions/38345932
复制相似问题