RODBC is the main library in R to import data from a database into R. RODBC seems to have the ability of "guess" the datatype of the column which I find it particularly annoying.
I have uploaded a file test.xls here, or you may create a xls file yourself:
- create 2 columns, first column named
col_aand the second column namedcol_b. - type whatever you like in
col_a, I typed letters on this column for 92 rows - at the 92th row of col_b, type a number there, I typed "1923" without changing the data type (i.e. not using
') - try to import the xls file into R using the following script:
library(RODBC)
setwd("C:/Users/hke775/Documents/Enoch/MISC/R_problems/RODBC")
channel <- odbcConnectExcel("test.xls",readOnly=TRUE)
dummy.df <- sqlFetch(channel,"Sheet1")
odbcClose(channel)
You will see that in dummy.df, col_b is all NA, the 1923 in this column is gone.
If you want to see the 1923 again, you can change the 1st row of col_b to a number, and it is back again.
This is very annoying as I don’t prefer modifying data manually. I need to use other package to do the xls importing, but I can’t find other packages do as smooth as RODBC (I tried gdata and xlsReadWrite).
Did I missing anything in the sqlFetch command, and cause the trouble? Thanks.
Please don’t blame R or RODBC for Microsoft’s bugs… 😉
I tried the fix in KB189897 by setting the
TypeGuessRowsvalue to 0 and look what happens!Please, no up-votes or check marks… just send cash. 🙂