aPaulT
aPaulT

Reputation: 593

Specifying column classes in read.csv.sql

When reading in a .csv using read.csv.sql from the sqldf package, is it possible to specify the column classes rather than having the function guess at them from the contents?

Supposing I have a .csv file too large to read using base read.csv, with a column I know to be character class but that almost always contains numeric values. Then no positive value of nrows will be guaranteed to catch the non-numeric values and thus assign the correct class, and nrows=-1 will load the entire colum vecotr into R, avoiding doing which is the reason I'm using read.csv.sql.

Upvotes: 1

Views: 421

Answers (1)

Roman Luštrik
Roman Luštrik

Reputation: 70653

This is the example from sqldf home page.

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"))))

Upvotes: 1

Related Questions