“Coercing argument of type ‘character’ to logical” 警告消息适用于 !any 函数

huangapple go评论81阅读模式
英文:

"Coercing argument of type 'character' to logical" warning message for !any function

问题

I run this code to see if in depth I have "0-5cm", it works but I also get a warning message that I don't understand, anyone know?

if (isTRUE(!any(depth) %in% "0-5cm")) {print("yes")} else {print("no")}
[1] "yes"
Warning message:
In any(depth) : coercing argument of type 'character' to logical

我运行这段代码来查看depth中是否有 "0-5cm", 它可以运行,但我也收到了一个我不理解的警告消息,有人知道吗?

if (isTRUE(!any(depth) %in% "0-5cm")) {print("yes")} else {print("no")}
[1] "yes"
Warning message:
In any(depth) : coercing argument of type 'character' to logical
英文:

I run this code to see if in depth I have "0-5cm", it works but I also get a warning message that I don't understand, anyone know?

if (isTRUE(!any(depth) %in% "0-5cm")) {print("yes")} else {print("no")}
[1] "yes"
Warning message:
In any(depth) : coercing argument of type 'character' to logical

答案1

得分: 1

请注意你的 any 后面的闭括号。由于 any 的输出已经是逻辑值,你不需要 isTRUE。我不确定你是否有意在 any 前面加了取反符号 (!)。

depth <- c(letters, "0-5cm")
[1] "a"     "b"     "c"     "d"     "e"     "f"     "g"    
[8] "h"     "i"     "j"     "k"     "l"     "m"     "n"    
[15] "o"     "p"     "q"     "r"     "s"     "t"     "u"    
[22] "v"     "w"     "x"     "y"     "z"     "0-5cm"

if ("0-5cm" %in% depth) {print("yes")} else {print("no")}
[1] "yes"
英文:

Note the closing bracket of your any. You don't need isTRUE since the output of any is already logical. Not sure if you're intensionally negating (!) any.

depth &lt;- c(letters, &quot;0-5cm&quot;)
[1] &quot;a&quot;     &quot;b&quot;     &quot;c&quot;     &quot;d&quot;     &quot;e&quot;     &quot;f&quot;     &quot;g&quot;    
[8] &quot;h&quot;     &quot;i&quot;     &quot;j&quot;     &quot;k&quot;     &quot;l&quot;     &quot;m&quot;     &quot;n&quot;    
[15] &quot;o&quot;     &quot;p&quot;     &quot;q&quot;     &quot;r&quot;     &quot;s&quot;     &quot;t&quot;     &quot;u&quot;    
[22] &quot;v&quot;     &quot;w&quot;     &quot;x&quot;     &quot;y&quot;     &quot;z&quot;     &quot;0-5cm&quot;

if (any(&quot;0-5cm&quot; %in% depth)) {print(&quot;yes&quot;)} else {print(&quot;no&quot;)}
[1] &quot;yes&quot;

答案2

得分: 1

你的括号使用有些混淆。
如果你想评估你的 if 条件,你需要更像这样:

if (!any(depth %in% "0-5cm")) # 寻找特定的在其中在逻辑上有点不合理,即使它起作用

此外,不知道 depth 的结构是什么样的,我假设它是一个向量?
如果是这种情况,我建议将你的语句改为:

!any("0-5cm" %in% depth) # 寻找多个中的特定内容
英文:

You are mixing up your parenthesis.
If you want to asses your if condition you need something more like:

if(!any(depth %in% &quot;0-5cm&quot;)) # looking for the many in the specific makes logically little sense, even if it works

Also not knowing how depth looks like, i assume it is a vector?
If this is the case I'd advise to turn your statement around to:

!any(&quot;0-5cm&quot; %in% depth) # looking for the specific within the many

huangapple
  • 本文由 发表于 2023年3月3日 18:02:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定