mutate_if在R中与case_when一起使用的多个条件

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

mutate_if multiple conditions along with case_when in R

问题

我想对那些是因子且只有2个水平的列应用case_when函数,而且它们的名称不包含"kiwi"。

这不起作用。我可以通过其他较长的方法得到我想要的结果,但我想知道是否有一种高效的方法来做到这一点。

library(dplyr)
library(stringr)

df <- data.frame(orange = factor(c(1,1,0,1,0)),
                 apple = factor(c("x", "x", "y", "y", "x")),
                 peach = 1:5,
                 kiwi = factor(c("a", "a", "b", "b", "c")))

df2 <- df %>%
    mutate_if(~ is.factor(.) & nlevels(.) == 2 & !str_detect(names(.), "kiwi"),
               ~ case_when(.x == 0 ~ "No",
                           .x == 1 ~ "Yes",
                           TRUE ~ as.character(.x)))

mutate_if在R中与case_when一起使用的多个条件

英文:

I want to apply the case_when function over those columns who are factors and have only 2 levels and whose names do not contain "kiwi".

This does not work. I can get what I want with other longer approaches but I was wondering if there is any way to do this is an efficient way.

librare(dplyr)
library(stringr)

df &lt;- data.frame(orange = factor(c(1,1,0,1,0)),
                 apple = factor(c(&quot;x&quot;, &quot;x&quot;, &quot;y&quot;, &quot;y&quot;, &quot;x&quot;)),
                 peach = 1:5,
                 kiwi = factor(c(&quot;a&quot;, &quot;a&quot;, &quot;b&quot;, &quot;b&quot;, &quot;c&quot;)))

df2 &lt;- df %&gt;%
    mutate_if((~ is.factor(.) &amp; nlevels(.) == 2 &amp; str_detect(names(.), &quot;kiwi&quot;, negate = TRUE)),
                     ~ dplyr::case_when(.x == 0, &quot;No&quot;,
                                        .x == 1 ~ &quot;Yes&quot;,
                                        TRUE ~ .x))

mutate_if在R中与case_when一起使用的多个条件

答案1

得分: 3

对一组变量进行操作中提到

作用域动词(_if, _at, _all)已被使用现有动词中的 pick()across() 取代。有关详情,请参阅 vignette("colwise")

使用 across(),您可以使用&lt;tidy-select&gt; 语法来保留或删除列:

  • 因子列:where(is.factor)
  • 仅有 2 个水平的列:where(~ nlevels(.x) == 2)
  • 列名不包含 "kiwi" 的列:!contains("kiwi")
df %>%
  mutate(across(where(~ is.factor(.x) & nlevels(.x) == 2) & !contains("kiwi"),
                ~ case_when(.x == 0 ~ "No",
                            .x == 1 ~ "Yes",
                            TRUE ~ .x)))
英文:

From Operate on a selection of variables, it said

> Scoped verbs (_if, _at, _all) have been superseded by the use of pick() or across() in an existing verb. See vignette(&quot;colwise&quot;) for details.

With across(), you can use the &lt;tidy-select&gt; syntax to keep or drop columns:

  • columns which are factors: where(is.factor)
  • have only 2 levels: where(~ nlevels(.x) == 2)
  • columns whose names do not contain "kiwi": !contains(&quot;kiwi&quot;)
df %&gt;%
  mutate(across(where(~ is.factor(.x) &amp; nlevels(.x) == 2) &amp; !contains(&quot;kiwi&quot;),
                ~ case_when(.x == 0 ~ &quot;No&quot;,
                            .x == 1 ~ &quot;Yes&quot;,
                            TRUE ~ .x)))

huangapple
  • 本文由 发表于 2023年3月7日 22:26:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663247.html
匿名

发表评论

匿名网友

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

确定