RODBC: chars and numerics converted aggressively (with/without as.is)

柔情痞子 提交于 2019-11-30 20:58:21
nrussell

If I understand correctly, I think this if what you are looking for,

str(sqlQuery(
  .conn, 
  "SELECT * FROM r2test", 
  stringsAsFactors = FALSE,
  as.is = c(TRUE, FALSE)
))
#'data.frame':  2 obs. of  2 variables:
# $ mychar: chr  "1" "2"
# $ mynum : num  3.14 6.28

where as.is is specified as a logical vector (must have the same length as the number of columns in the result set). To be fair, this isn't really spelled out very well. The man page for sqlQuery just refers you to the as.is argument in read.table, which states:

Note that as.is is specified per column (not per variable) and so includes the column of row names (if any) and any columns to be skipped.

The downside of this approach is that you need to know in advance which columns you want to convert and which you don't. Personally I don't see why the default behavior isn't to just map SQL character types to R character types, SQL numeric types to R numeric types, etc, but perhaps there is a good reason for this on the backend. Automatically converting '1', '2', ... to integers does not seem like much of a "feature" to me.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!