[R] sqldf() difference between R 3.1.2 and 3.0.1
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Feb 11 16:22:10 CET 2015
On Wed, Feb 11, 2015 at 9:45 AM, Doran, Harold <HDoran at air.org> wrote:
> I have a function written and tested using R 3.0.1 and sqldf_0.4-7.1 that works perfectly. However, using this same code with R 3.1.2 and sqldf_0.4-10 yields the error below that I am having a difficult time deciphering. Hence, same code behaves differently on different versions of R and sqldf().
>
> Error in sqliteSendQuery(con, statement, bind.data) :
> error in statement: no such column: V1
>
>
> Reproducible example below as well as complete sessionInfo all provided below.
>
>
> My function and code using the function are below.
>
> dorReader <- function(dorFile, layout, sepChar = '\n'){
> sepChar <- as.character(sepChar)
> dorFile <- as.character(dorFile)
> layout$type2 <- ifelse(layout$type == 'C', 'character',
> ifelse(layout$type == 'N', 'numeric', 'Date'))
> dor <- file(dorFile)
> attr(dor, "file.format") <- list(sep = sepChar)
> getVars <- paste("select",
> paste("substr(V1, ", layout$Start, ", ",
> layout$Length, ") '", layout$Variable.Name, "'", collapse = ", "), "from dor")
> dat <- sqldf(getVars)
>
> classConverter <- function(obj, types){
> out <- lapply(1:length(obj),FUN = function(i){FUN1 <- switch(types[i],character = as.character,numeric = as.numeric,factor = as.factor, Date = as.character); FUN1(obj[,i])})
> names(out) <- colnames(obj)
> as.data.frame(out)
> }
> dat <- classConverter(dat, layout$type2)
> names(dat) <- layout$Variable.Name
> dat
> }
>
> ### contents of fwf file 'sample.txt'
> 1234567
> 1234567
> 1234567
> 1234567
> 1234567
> 1234567
> 1234567
> 1234567
>
> layout <- data.frame("Variable.Name" =c('test1', 'test2'), "Length" = c(3,4), "Start" =c(1,4), "End" = c(3,7), "type" = c('N', 'N'))
>
> tmp <- dorReader('sample.txt', layout)
sqldf is documented to use the sqliteImportFile defaults for
file.format components. It may be that RSQLite 1.0 has changed the
default for header in sqliteImportFile.
Try replacing your statement that sets file.format with this:
attr(dor, "file.format") <- list(sep = sepChar, header = FALSE)
More information about the R-help
mailing list