gpt4 book ai didi

r - 如何从 case_when() 返回多个值或一个小标题?

转载 作者:行者123 更新时间:2023-12-04 02:28:01 25 4
gpt4 key购买 nike

我找不到返回多个值(列)或仅来自 case_when() 的小标题的方法.

input <- tibble(a = c(1, 2, 3))

input %>%
mutate(
case =
case_when(
a == 1 ~ tibble(x = "case1", y = "c1"),
a == 2 ~ tibble(x = "case2", y = "c2"),
a == 3 ~ tibble(x = "case3", y = "c3")
)
)
如何根据某些正则表达式条件设置多个参数?

最佳答案

我们可以返回一个 list列为 mutate期望常规列是 vectorlength与原始数据的行数相同

input %>%
mutate(
case =
case_when(
a == 1 ~ list(tibble(x = "case1", y = "c1")),
a == 2 ~ list(tibble(x = "case2", y = "c2")),
a == 3 ~ list(tibble(x = "case3", y = "c3"))
)
)
-输出
# A tibble: 3 x 2
# a case
# <dbl> <list>
#1 1 <tibble [1 × 2]>
#2 2 <tibble [1 × 2]>
#3 3 <tibble [1 × 2]>

关于r - 如何从 case_when() 返回多个值或一个小标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66093186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com