r警告'长度(x)= 2> 1'在强制质量(逻辑(1)'
使用r 4.1.3我观察到:
var <- 0
> if(is.data.frame(var) || is.vector(var)) var <- as.matrix(var)
> is.null(var) || (!is.matrix(var) && var == 0) || (dim(var)==c(1,1) && var[1,1]==0)
[1] TRUE
但是,在r 4.2.1时,我会在同一代码上观察警告,
> var <- 0
> if(is.data.frame(var) || is.vector(var)) var <- as.matrix(var)
> is.null(var) || (!is.matrix(var) && var == 0) || (dim(var)==c(1,1) && var[1,1]==0)
[1] TRUE
Warning message:
In dim(var) == c(1, 1) && var[1, 1] == 0 :
'length(x) = 2 > 1' in coercion to 'logical(1)'
很难在此处找到根本原因。现在发生的原因有什么想法,这是一个很好的解决方案吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
dim(var)== c(1,1)
给出两个true
。请参阅下面的 r 新闻。 以前可能会替换,我们被告知
&amp;&amp;
and||
在>()
中都可以安全地使用是还是错。但是现在它会警告您。这实际上使检测错误变得更容易,因此我对此更改感到满意。不幸的是,此更改影响了一些 r 软件包(例如,请参见:由于“如果在“错误:条件具有长度&gt; 1 )。结果,过去使用顺利进行的代码突然发出警告甚至错误。
dim(var) == c(1,1)
gives twoTRUE
. See R News below. Possible replacements arePreviously we were told that
&&
and||
are safe to use inif ()
as they silently return a single TRUE or FALSE. But now it will warn you. This actually makes it easier to detect bugs, so I am happy with this change.Unfortunately, this change has affected a few R packages (see for example: Package ceases to work due to "if" error: condition has length > 1). As a result, the code that used to work smoothly suddenly throws warnings or even errors.
我只是在同一问题中跑了。我使用简单的&amp;解决了问题。而不是双重。
从
这个
I just ran in the same issue. I fixed my problem using a simple & rather than a double.
From this
to this