1

read.csv.sql从包中读取 .csv 时sqldf,是否可以指定列类而不是让函数从内容中猜测它们?

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

4

1 回答 1

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"))))
于 2013-05-02T13:26:07.897 回答