【问题标题】:Why does as.integer64("") return 0 instead of NA_integer64_?为什么 as.integer64("") 返回 0 而不是 NA_integer64_?
【发布时间】:2017-09-20 21:34:02
【问题描述】:

假设空字符串的基本as.integer() 强制是NA 没有警告,如:

str( as.integer(c('1234','5678','')) ) # int [1:3] 1234 5678 NA -- no warning

我很难理解为什么bit64::as.integer64() 在没有警告的情况下强制为零:

library('bit64')
str( as.integer64(c('1234','5678','')) ) # integer64 [1:3] 1234 5678 0 -- no warning

比较奇怪的是:

str( as.integer(c('1234','5678','', 'Help me Stack Overflow')) ) 
# int [1:4] 1234 5678 NA NA -- coercion warning

与:

str( as.integer64(c('1234','5678','', 'Help me Stack Overflow')) ) 
# integer64 [1:4] 1234 5678 0 NA -- no warning

我的解决方法非常失败:

asInt64 <- function(s){
  require(bit64)
  ifelse(grepl('^\\d+$',s), as.integer64(s), NA_integer64_)
}
str(asInt64(c('1234','5678','', 'Help me Stack Overflow')) )
# num [1:4] 6.10e-321 2.81e-320 0.00 0.00
# huh?

所以,我问:

  • 为什么会这样?

  • 最好的解决方法是什么?

【问题讨论】:

  • 可能是因为strtoll("", ...)0。解决方法可能是之后将这些grepl("\\D|^$", c('1234','5678','', 'Help me Stack Overflow')) 转换为NA
  • @lukeA 你基本上回答了这个问题,谢谢。这个reference 建议在strtoll 返回0 时测试转换错误,as.integer64endpointer 逻辑不太相符。我将尝试提出更改,尽管我的 C 很生锈。如果您想发表您的评论作为答案,我会接受。

标签: r bit64


【解决方案1】:

为什么会发生

正如@lukeA 的评论所指出的,as.integer64.character 的来源是:

SEXP as_integer64_character(SEXP x_, SEXP ret_){
  long long i, n = LENGTH(ret_);
  long long * ret = (long long *) REAL(ret_);
  const char * str;
  char * endpointer;
  for(i=0; i<n; i++){
    str = CHAR(STRING_ELT(x_, i)); endpointer = (char *)str; // thanks to Murray Stokely 28.1.2012
    ret[i] = strtoll(str, &endpointer, 10);
    if (*endpointer)
      ret[i] = NA_INTEGER64;
  }
  return ret_;
}

并且strtoll("") 在调用无效值(例如"""ABCD")时返回零并出错。一位参考strtoll example 处理如下:

/* If the result is 0, test for an error */
if (result == 0)
{
    /* If a conversion error occurred, display a message and exit */
    if (errno == EINVAL)
    {
        printf("Conversion error occurred: %d\n", errno);
        exit(0);
    }

    /* If the value provided was out of range, display a warning message */
    if (errno == ERANGE)
        printf("The value provided was out of range\n");
}

所以我现在想弄清楚为什么*endpointer 评估为 FALSE。 (敬请期待……)

解决方法

这里是模仿基本as.integer 行为的解决方法:

library(bit64)
charToInt64 <- function(s){
  stopifnot( is.character(s) )
  x <- as.integer64(s)
  # as.integer64("") unexpectedly returns zero without warning.  
  # Overwrite this result to return NA without warning, similar to base as.integer("")
  x[s==""] <- NA_integer64_
  # as.integer64("ABC") unexpectedly returns zero without warning.
  # Overwrite this result to return NA with same coercion warning as base as.integer("ABC")
  bad_strings <- grepl('\\D',s) # thanks to @lukeA for the hint
  if( any(bad_strings) ){
    warning('NAs introduced by coercion')
    x[bad_strings] <- NA_integer64_  
  }
  x
}

要看看这是否有效:

test_string <- c('1234','5678','', 'Help me Stack Overflow')
charToInt64(test_string) # returns int64 [1] 1234 5678 <NA> <NA> with warning
charToInt64(head(test_string,-1)) # returns int64 [1] 1234 5678 <NA> without warning

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-24
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2015-05-05
    相关资源
    最近更新 更多