英文:
"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 <- 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 (any("0-5cm" %in% depth)) {print("yes")} else {print("no")}
[1] "yes"
答案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% "0-5cm")) # 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("0-5cm" %in% depth) # looking for the specific within the many
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论