3

我正在尝试从R的 UCI 存储库中收集公开可用的数据集。我知道有很多数据集已经可以用于几个R包,例如但是我仍然需要来自 UCI 存储库的几个数据集。mlbench.

这是我学到的一个技巧

url="http://archive.ics.uci.edu/ml/machine-learning-databases/credit-screening/crx.data"
credit<-read.csv(url, header=F)

但这并没有得到标题(变量名)信息。该信息以文本格式保存在*.names文件中。知道如何以编程方式获取标题信息吗?

4

2 回答 2

3

我怀疑您必须使用正则表达式来完成此操作。这是一个丑陋但通用的解决方案,它应该适用于各种 *.names 文件,假设它们的格式与您发布的格式相似。

names.file.url <-'http://archive.ics.uci.edu/ml/machine-learning-databases/credit-screening/crx.names' 
names.file.lines <- readLines(names.file.url)

# get run lengths of consecutive lines containing a colon.
# then find the position of the subgrouping that has a run length 
# equal to the number of columns in credit and sum the run lengths up 
# to that value to get the index of the last line in the names block.
end.of.names <- with(rle(grepl(':', names.file.lines)), 
                       sum(lengths[1:match(ncol(credit), lengths)]))

# extract those lines
names.lines <- names.file.lines[(end.of.names - ncol(credit) + 1):end.of.names]

# extract the names from those lines
names <- regmatches(names.lines, regexpr('(\\w)+(?=:)', names.lines, perl=TRUE))

# [1] "A1"  "A2"  "A3"  "A4"  "A5"  "A6"  "A7"  "A8"  "A9"  "A10" "A11"
# [12] "A12" "A13" "A14" "A15" "A16"
于 2012-11-08T18:23:01.267 回答
1

我猜这Attribute Information一定是您指向的特定文件中的名称。这是一个非常非常肮脏的解决方案。我使用了一个事实,即有一个模式 - 你的名字后面跟着,所以我们使用:分隔字符串,然后从原始向量中获取名称::scan

url="http://archive.ics.uci.edu/ml/machine-learning-databases/credit-screening/crx.data"
credit<-read.csv(url, header=F)
url.names="http://archive.ics.uci.edu/ml/machine-learning-databases/credit-screening/crx.names"
mess <- scan(url.names, what="character", sep=":")
#your names are located from 31 to 61, every second place in the vector
mess.names <- mess[seq(31,61,2)]
names(credit) <- mess.names
于 2012-11-08T18:19:31.130 回答