2017-10-16 60 views
1

我一直在使用来自UCI Machine Learning Repository的数据集。一些数据集,如this one,包含a file,扩展名为.c45-names,看起来机器可读。从名称文件中自动导入R中的列名称

有没有办法使用这些数据自动命名数据框中的列,或者甚至更好地使用其他元数据,如数据类型或离散变量的可能值?

目前,我复制/粘贴列名成这样一行代码:

names(cars) = c('buying', 'maint', 'doors', 'persons', 'lug_boot', 'safety', 'rating') 

它会很好,如果有更多的东西自动化,谷歌搜索迄今一直效果不佳,因为有一个这是一个在河

回答

1
car.c45_names <- readLines("https://archive.ics.uci.edu/ml/machine-learning-databases/car/car.c45-names") 
tmp <- car.c45_names[grep(":", car.c45_names)] #grab lines containing ":" 
colname_car.c45 <- sub(':.*', '', tmp) #replace all characters after ":" with ""; thanks to alistaire's for pointing out  
# colname_car.c45 <- sapply(tmp, function(x)substring(x, 1, gregexpr(":", x)[[1]]-1)) 
cars <- setNames(cars, colname_car.c45) #same as 'names(cars) <- colname_car.c45' 
+0

已经实现你可以做'colname_car.c45 <类似名称的分类算法 - 子( ':*', '',TMP)' – alistaire

+0

感谢您指出。我在上面的答案中插入了它。 –