在read.csv.sql中指定列类

时间:2013-05-02 13:22:22

标签: r sqldf

使用read.csv.sql包中的sqldf读取.csv时,是否可以指定列类而不是从内容中猜出函数?

假设我的.csv文件太大而无法使用基类read.csv读取,我知道列是字符类,但几乎总是包含数值。然后,nrows的正值将不会保证捕获非数字值,从而分配正确的类,nrows=-1将整个colum vecotr加载到R中,避免这样做,这就是我' m使用read.csv.sql

1 个答案:

答案 0 :(得分:1)

这是sqldf主页的示例。

library(sqldf)

# example example 8a - file.format attribute on file.object

numStr <- as.character(1:100)
DF <- data.frame(a = c(numStr, "Hello"))
write.table(DF, file = "~/tmp.csv", quote = FALSE, sep = ",")
ff <- file("~/tmp.csv")

attr(ff, "file.format") <- list(colClasses = c(a = "character"))

tail(sqldf("select * from ff"))


# example 8b - using file.format argument

numStr <- as.character(1:100)
DF <- data.frame(a = c(numStr, "Hello"))
write.table(DF, file = "~/tmp.csv", quote = FALSE, sep = ",")
ff <- file("~/tmp.csv")

tail(sqldf("select * from ff",
 file.format = list(colClasses = c(a = "character"))))