【问题标题】:bit64 NA doesn't replicate in data.frame constructorbit64 NA 不会在 data.frame 构造函数中复制
【发布时间】:2020-08-06 15:09:27
【问题描述】:

在构建数据框时,如果长度不同,列会被复制。

> data.frame(x = c(1,2), y = NA_integer_)
  x  y
1 1 NA
2 2 NA

但是,当我尝试使用 bit64::NA_integer64_ 执行此操作时,出现错误。有谁知道会发生什么?如果rep()bit64::NA_integer64_ 上单独调用,则rep() 有效。

> data.frame(x = c(1,2), y = bit64::NA_integer64_)
Error in data.frame(x = c(1, 2), y = bit64::NA_integer64_) : 
  arguments imply differing number of rows: 2, 1
> rep(bit64::NA_integer64_, 2)
integer64
[1] <NA> <NA>

【问题讨论】:

标签: r dataframe bit64


【解决方案1】:

data.frame 只会回收:

  • names 之外没有其他属性的向量
  • factor
  • AsIscharacter
  • Date
  • POSIXct

tibble 没有这个问题。

tibble::tibble(x = c(1,2), y = bit64::NA_integer64_)
#> # A tibble: 2 x 2
#>       x       y
#>   <dbl> <int64>
#> 1     1      NA
#> 2     2      NA

这是来自data.frame的相关sn-p

for (i in seq_len(n)[nrows < nr]) {
    xi <- vlist[[i]]
    if (nrows[i] > 0L && (nr%%nrows[i] == 0L)) {
        xi <- unclass(xi)
        fixed <- TRUE
        for (j in seq_along(xi)) {
            xi1 <- xi[[j]]
            if (is.vector(xi1) || is.factor(xi1)) 
              xi[[j]] <- rep(xi1, length.out = nr)
            else if (is.character(xi1) && inherits(xi1, "AsIs")) 
              xi[[j]] <- structure(rep(xi1, length.out = nr), 
                class = class(xi1))
            else if (inherits(xi1, "Date") || inherits(xi1, "POSIXct")) 
              xi[[j]] <- rep(xi1, length.out = nr)
            else {
              fixed <- FALSE
              break
            }
        }
        if (fixed) {
            vlist[[i]] <- xi
            next
        }
    }
    stop(gettextf("arguments imply differing number of rows: %s", 
        paste(unique(nrows), collapse = ", ")), domain = NA)
}

【讨论】:

    猜你喜欢
    • 2013-10-13
    • 1970-01-01
    • 2012-10-10
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多