RODBC:为什么是“价值观”;不适用;对于sqlQuery()中的空值和仅空格值?

RODBC:为什么是“价值观”;不适用;对于sqlQuery()中的空值和仅空格值?,r,odbc,rodbc,R,Odbc,Rodbc,对于R,我是一个新手。我正在查看以下返回的RODBC结果: > library(RODBC) > dbcon <- odbcDriverConnect("DRIVER={SQL SERVER};SERVER=MYSERV;DATABASE=SOME", tabQuote='', colQuote='') > sqlQuery(dbcon, "SELECT 3, 'a', ' ', '', NULL") .1 .2 .3 .4 1 3 a NA NA NA >库

对于R,我是一个新手。我正在查看以下返回的RODBC结果:

> library(RODBC)
> dbcon <- odbcDriverConnect("DRIVER={SQL SERVER};SERVER=MYSERV;DATABASE=SOME", tabQuote='', colQuote='')
> sqlQuery(dbcon, "SELECT 3, 'a', ' ', '', NULL")
    .1 .2 .3 .4
1 3  a NA NA NA
>库(RODBC)
>dbcon sqlQuery(dbcon,“选择3,'a','',NULL”)
.1 .2 .3 .4
1 3 a不,不,不

为什么
'
'
返回
NA
?我在文档中找不到有关此行为的任何信息。我遗漏了什么?

因此决定查看RODBC的
sqlQuery
方法的源代码,因为我记得它是开源的:

sqlQuery <-
    function(channel, query, errors = TRUE, ..., rows_at_time)
{
    if(!odbcValidChannel(channel))
       stop("first argument is not an open RODBC channel")
    if(missing(query))
        stop("missing argument 'query'")
    ## could argue that 'max' should restrict rows_at_time
    rows_at_time <- if(missing(rows_at_time)) attr(channel, "rows_at_time")
    else max(1, min(1024, rows_at_time))
    stat <- odbcQuery(channel, query, rows_at_time)
    if(stat == -1L) {
        if(errors) return(odbcGetErrMsg(channel))
        else return(invisible(stat))
    } else return(sqlGetResults(channel, errors = errors, ...))
}
因此,对于string/varchar值,可以归结为:

data[[i]] <- type.convert(as.character(data[[i]]),
                                         na.strings = na.strings,
                                          as.is = !stringsAsFactors,
                                          dec = dec)
好的,这是意料之中的。现在让我们来试试这些奇怪的例子:

> type.convert("")
[1] NA
嗯。。。好的,这就是我们得到的
NA

> type.convert("       ")
[1] NA
好的,那也是
NA
。 现在,为什么
as.is
不返回
NA

if(as.is[i] || is.list(data[[i]])) next
啊哈,它只是在设置了
as.is
时退出,不调用
type.convert()

这就解释了当设置了
as.is
标志时,它为什么不返回
NA

您可以使用
as.is
option@Hack-R:那会管用的,但我不明白为什么它一开始就试图转换成NA,而我没有通过类似NA的字符串告诉它。是否有一些全局参数可以说明这一点?它怎么知道的?我不确定。这里有一个
nullstring
参数,但我认为从技术上讲,空格不是空的。我想这只是默认的行为,因为包作者认为更多的时候,这将是理想的解释。
> type.convert("       ")
[1] NA
if(as.is[i] || is.list(data[[i]])) next