Search code examples
rrodbc

Import database as factors or characters with package rodbc


I am trying to read an MS Access database with the package("RODBC").

library("RODBC")
db<-file.path("file.accdb")
channel<-odbcConnectAccess2007(db)
dataSetName<-sqlFetch(channel,"file")
close(channel)

It works, but when I have numeric values starting by 0, for instance 089, the package reads it as integer and converts it in 89, it removes the zero. So, I would like to read the entire database as factors or characters to avoid this. I was wondering if there is an argument to solve it. Thanks in advance.


Solution

  • To read the entire database as characters (if possible):

    dataSetName<-sqlFetch(channel,"file", as.is=TRUE) 
    

    For factors I am not completely sure. stringsAsFactors = TRUE might work.

    dataSetName<-sqlFetch(channel,"file", stringsAsFactors = TRUE)