【问题标题】:Error when performing an NA replacement in R 4.0在 R 4.0 中执行 NA 替换时出错
【发布时间】:2021-03-04 03:44:48
【问题描述】:

使用 R 3.6,我可以执行以下 NA 替换

> d <- zoo(data.frame(a = NA, b = 1), Sys.Date())
> d[is.na(d)] <- 1
> d
           a b
2021-03-03 1 1

使用 R 4.0 我收到以下错误:

> d <- zoo(data.frame(a = NA, b = 1), Sys.Date())
> d[is.na(d)] <- 1
Error in as.Date.default(e) : 
  do not know how to convert 'e' to class “Date”

在 R 4.0 中是否更改了某些默认行为?

R 3.6 会话信息:

Microsoft Windows [Version 10.0.19041.804]
(c) 2020 Microsoft Corporation. All rights reserved.

C:\>R --no-site-file

R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: i386-w64-mingw32/i386 (32-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(zoo)

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

Warning message:
package 'zoo' was built under R version 4.0.4
> d <- zoo(data.frame(a = NA, b = 1), Sys.Date())
> d[is.na(d)] <- 1
> d
           a b
2021-03-03 1 1

R 4.0 会话信息:

Microsoft Windows [Version 10.0.19041.804]
(c) 2020 Microsoft Corporation. All rights reserved.

C:\>R --no-site-file

R version 4.0.4 (2021-02-15) -- "Lost Library Book"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: i386-w64-mingw32/i386 (32-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(zoo)

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

> d <- zoo(data.frame(a = NA, b = 1), Sys.Date())
> d[is.na(d)] <- 1
Error in as.Date.default(e) :
  do not know how to convert 'e' to class "Date"

会话信息(3.6):

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] zoo_1.8-8

loaded via a namespace (and not attached):
[1] compiler_3.6.1  grid_3.6.1      lattice_0.20-38

会话信息(4.0):

> sessionInfo()
R version 4.0.4 (2021-02-15)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] zoo_1.8-8

loaded via a namespace (and not attached):
[1] compiler_4.0.4  tools_4.0.4     grid_4.0.4      lattice_0.20-41

【问题讨论】:

  • 我猜这与 R 的版本关系不大,而与您安装的 zoo 软件包的版本关系更大。如果你在加载 zoo 后运行sessionInfo(),你在不同的 R 控制台中使用的是什么版本?
  • 对于两者 --- 其他附加包:[1] zoo_1.8-8
  • 我找到了一个在线编译器 R3.4.4, zoo 1.8.1 并且可以复制旧的行为,可能确实是一个错误或更改了例程。试试d &lt;- na.fill(d, 1) 得到你想要的。
  • 我是here @MrFlick
  • @neilfws,是的。 Achim 看了看,确定 R 的变化是造成这种情况的原因。

标签: r zoo


【解决方案1】:

感谢您提出这个问题,这是zoo 包中的一个错误。在[.zoo[&lt;-.zoo 方法中,我们检查了索引i 是否是一个矩阵通过

if (all(class(i) == "matrix")) ...

这在 R 3.x.y 中可以正常工作,因为矩阵对象只有 "matrix" 类。但是,在 R 4.0.0 中,矩阵对象开始另外继承自 "array"。请参阅:https://developer.R-project.org/Blog/public/2019/11/09/when-you-think-class.-think-again/

在 R-Forge (https://R-Forge.R-project.org/R/?group_id=18) 上的 zoo 开发版本中,我现在通过将上述代码替换为

解决了这个问题
if (inherits(i, "matrix")) ...

因此,您已经可以从 R-Forge 安装 zoo 1.8-9,您的代码将再次按预期工作。或者,您可以等待该版本到达 CRAN,该版本有望在反向依赖检查后的几天内发布。同时,您可以使用

解决此问题
coredata(d)[is.na(d)] <- 1

【讨论】:

  • 这里下载安装zoo开发版:install.packages("zoo", repos="http://R-Forge.R-project.org")
  • 现在 CRAN 上也发布了 1.8-9 的固定版本:CRAN.R-project.org/package=zoo
【解决方案2】:

我也有这个问题!!关于可能发生的事情,这里有更多奇怪的行为/面包屑。仍然不确定为什么,但似乎是一个带有 zoo 而不是 is.na() 的索引问题。如果逻辑结构具有与动物园 obj 相同的行名/索引,则逻辑索引有效:

  1. 打印出d[is.na(d)](没有赋值)会导致一个空的动物园对象,这表明问题出在索引上

  2. 在 coredata() 中包装 d 有效

d <- zoo(data.frame(a = NA, b = 1), Sys.Date())
coredata(d)[is.na(d)] <- 1
d
             a b
> 2021-03-05 1 1
  1. is.na() 返回的逻辑如果转换为与 zoo obj 具有相同的行名/索引,则将起作用。
d <- zoo(data.frame(a = NA, b = 1), Sys.Date())
changes <- is.na(d) #storing logical in a variable
> d
            a b
2021-03-05 NA 1

> changes #d[changes] won't work, so change rownames 
        a     b
[1,] TRUE FALSE

> changes <- as.zoo(changes, index(d))
> changes
              a     b
2021-03-05 TRUE FALSE

> d[as.logical(changes)] #changing zoo back to a logical, returns something 
            a b
2021-03-05 NA 1

> d[as.logical(changes)] <- 1
            a b
2021-03-05 NA 1

现在是面包屑...有人知道版本 4 中对 R 的日期类进行了哪些更改吗? Zoo 建议它对 merge.zoo 进行了一些更改,“明确解决 R >= 4.1.0 中 c.Date() 的新行为。”

https://cran.r-project.org/web/packages/zoo/NEWS(请参阅顶部的第 2 点)

我已经搜索和搜索,但没有看到任何提及这些更改...

我猜 zoo 类有一些更改,以更严格地执行日期索引...不确定...似乎也无法确定发布与 zoo 相关的问题

更多面包屑.... 根据这个线程https://github.com/joshuaulrich/xts/issues/331

,显然这个功能早在 2020 年 4 月就适用于动物园对象

R 4.0.1 于当月晚些时候发布,最新的 zoo 包 1.8-8 于 2020 年 5 月发布,因此运行该包的 1.8-7 版可能可以确定它是带 R 的更改还是带 w/ 的更改导致不同行为的动物园

【讨论】:

    【解决方案3】:

    @neilfws 你说得对,这个问题是由于 R 4.0 中类响应的变化造成的。

    目前,最好的选择是使用以下任一:

    d &lt;- na.fill(d, 1)

    coredata(d)[is.na(d)] &lt;- 1

    d[is.na(d)] &lt;- 1 的旧用例需要更新 zoo 包

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-25
      • 2021-07-02
      • 2019-07-10
      • 2013-01-09
      • 1970-01-01
      相关资源
      最近更新 更多