Case_为什么在这里有两个不同的输出?

发布于 2025-02-11 17:49:30 字数 559 浏览 1 评论 0原文

时,我无法理解为什么输出有所不同

当我使用两种不同的case_时:选项1:

x = 5
y = print("goodmorning")
z = print("goodafternoon")
q = print("goodevening")
case_when(x > 3 ~ y, x < 8 ~ z) 

输出1:

[1] "goodmorning"

选项2:

x = 5
case_when(x > 3 ~ print("goodmoring"), x < 8 ~ print("goodafternoon"))

输出2:

[1] "goodmoring"
[1] "goodafternoon"
[1] "goodmoring"

有人可以启发我如何导致两个完全不同的输出 。我相信case_时,当第一次满足A条件时,它会从Case_case跳出,并且这似乎是第一个代码的,但是为什么它不适合第二个代码呢?

I am failing to understand why the output is different when I use two different cases of case_when:

Option 1:

x = 5
y = print("goodmorning")
z = print("goodafternoon")
q = print("goodevening")
case_when(x > 3 ~ y, x < 8 ~ z) 

Output 1:

[1] "goodmorning"

Option 2:

x = 5
case_when(x > 3 ~ print("goodmoring"), x < 8 ~ print("goodafternoon"))

Output 2:

[1] "goodmoring"
[1] "goodafternoon"
[1] "goodmoring"

Can someone enlighten me how this results in two completely different outputs. I believed that case_when sequentially and that it jumps out of the case_case when the first time the a condition is met and this seems to hold for the first piece of code, but why does it not hold for the second code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一江春梦 2025-02-18 17:49:30

引用帮助页面(强调我的),

case_when()评估所有RHS表达式,然后通过提取所选的(通过LHS表达式)零件来构建其结果

Citing the help page (emphasis mine),

case_when() evaluates all RHS expressions, and then constructs its result by extracting the selected (via the LHS expressions) parts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文